So here's a strange issue I've encountered; I have a script that's trying to build a set of commands to run via eval
, and these need to run within a sub-shell as part of a bunch of other commands (so I can run them asynchronously). These commands include quotations to protect values with spaces etc. inside, however they're being treated as if the quotations aren't there at all.
For example:
cmd="echo 'Foo Bar'"
$(eval "$cmd" | sort) &
Results an error of "Foo: command not found"
While running it more directly works fine like-so:
eval "$cmd" | sort
I realise the example isn't very interesting, but what I don't understand is why the quotations are being lost and how to avoid it, while still executing in a proper sub-shell (with and without processing in the background).
you need to drop the $
, $(eval "$cmd" | sort) &
causes bash to attempt to execute the output from a command substitution
Try (eval "$cmd" | sort) &
instead