help-bash
[Top][All Lists]
Advanced

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

Running commands as "$@" ( ERRATUM )


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


> This question is intended to replace one with the same subject due to an 
error.
>
> The error was here.
>
> ---- wrong fragment ----
>
> # ---- begin script runner.sh ----
> eval "$@"
> # ---- end script runner.sh ----
>
> The fragment below is ok.
>
> Sorry for this.




Hello all

Let's assume the following script.

# ---- begin script runner.sh ----
"$@"
# ---- end script runner.sh ----

If we call this script with the following command line

    $ sh runner.sh ls -l

the 'ls -l' command will be executed.

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)?

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.

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]