On Thu, 2 Feb 2023 12:11:32 +0100, Cristian Zoicas
<zoicas@medialab.sissa.it> wrote:
Now, if a call the script with the commands
$ sh runner.sh A=B
we get the following errors:
runner.sh: 10: runner.sh: A=B: not found
Why the shell does not treat the first string in "$@" ('A=B'
in these cases) as the first possibile token of command (which
is an assignemnt)?
It works for me ("sh" points to bash).
In addition, the problem is NOT solved by using 'eval "$@"'.
Apparently it works. For example the command
$ sh runner.sh A=BCD
works, but
$ sh runner.sh A="B CD"
gives an error. In this case the error (I think) comes
from the way the expansion of "$@" is performed: eval has to evaluate the
string "A=B CD", so it treats A=B as an assignemnt and 'CD' as
a command.
This happens because the quotes are removed by the calling shell before eval
has a chance to see them. This works:
$ sh runner.sh 'A="B CD"'
or even, to check
$ sh runner.sh 'A="B CD"; echo "$A"'
B CD
I am curios what is going on and if there is any possibility of
running a command (any command, including an assignment) by using
a very simple invocation (someting like eval "$@").
The short answer is: yes, but not where quoting is involved unless you take
special measures to ensure that the relevant quotes are seen by eval.