1
I have a Linux bash script, to the effect of:
COMMAND_TO_EXECUTE="foo "$1" --option1 --option2 "$2
exec $COMMAND_TO_EXECUTE
Problem: the script fails
Debugging clue, If I echo $COMMAND_TO_EXECUTE
then cut/paste echo that into a terminal window, it works perfectly.
So.. The string behind $COMMAND_TO_EXECUTE
is valid in a terminal, but not valid in a script. Is there something I should do to $COMMAND_TO_EXECUTE
before trying to executing it?
Amplifying info: the commands to execute have been wget
or curl
, I have the same problems with both. (I have properly quoted strings and escaped characters like &). As mentioned before.. the command works fine if I echo, then cut/paste it.
I am puzzled and feel I am missing something elementary because the command works cut/pasted but not in script.
UPDATE: Polyergic's reply below worked for me. bash -c "$COMMAND_TO_EXECUTE"
runs properly.
As a side note, why do you have the variables outside the double quotes? – ShadSterling – 2014-12-14T02:14:34.200
The quotes delimit the text that I intended to concatenate the variables to. – Paulb – 2014-12-14T08:41:26.097
Thinking of it in terms of concatenation doesn't really match what Bash is doing. It's not like "real programming languages" where you store values and use operators, it's just manipulating that line of text. (I'd call it a string, but it's misleading to think of it as a string in the sense of other languages.) In short, it's more idiomatic and marginally better to do the variable expansion inside the doublequoted string. – ShadSterling – 2014-12-14T18:41:59.480