[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Running commands as "$@"

From: Cristian Zoicas
Subject: Running commands as "$@"
Date: Thu, 2 Feb 2023 12:11:32 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0 SeaMonkey/2.53.3

Hello all

Let's assume the following script.

# ---- begin script ----
eval "$@"
# ---- end script ----

If we call this script with the following command line

    $ sh ls -l

the 'ls -l' command will be executed.

Now, if a call the script with the commands

    $ sh A=B

we get the following errors: 10: 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)?

In addition, the problem is NOT solved by using 'eval "$@"'.
Apparently it works. For example the command

     $ sh A=BCD

 works, but

     $ 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.

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 "$@").

Thank you in advance.
Cristian Zoicas

reply via email to

[Prev in Thread] Current Thread [Next in Thread]