[Top][All Lists]

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

Re: Running commands as "$@"

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

Davide Brini wrote:
On Thu, 2 Feb 2023 12:11:32 +0100, Cristian Zoicas
<> wrote:

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

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

This happens because the quotes are removed by the calling shell before eval
has a chance to see them. This works:

$ sh 'A="B    CD"'

or even, to check

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

Correct, but I want to avoid taking these special measures. It is (and it was) 
clear for
me why the script does not work with eval.

There was a mistake in my original question and I am extremely sorry for that.

I wanted to ask the question for this script

# ---- begin script ----
# ---- end script ----

and not for the one with "eval"

Here is what I want to obtain:

    A command is a (parsed) sequence of strings and also "$@" is a sequence
    of strings.

    So expected that "$@" would allow me to run anything that was a valid 

    In fact the script above lets me to run "ls -l" but fails to run "A=B"

Hope that clarifies my issue.

reply via email to

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