:: Re: [DNG] [OT] bash / quote weirdne…
Página Principal
Delete this message
Reply to this message
Autor: Antony Stone
Data:  
Para: dng
Assunto: Re: [DNG] [OT] bash / quote weirdness
On Wednesday 12 January 2022 at 00:08:38, Florian Zieboll via Dng wrote:

> #!/bin/bash
> for f in "$@" ; do
>     xcmd="unrar x"
>     $xcmd "$f"
> done

>
> Can please somebody explain, why, if I double-quote the "$xcmd"
> variable in line 4, the script fails with
>
>     ./test.sh: line 4: unrar x: command not found


Double-quoting turns the string into a single token, and therefore the parser
sees the line as:

    token 1 = "unrar x"
    token 2 = "$f"


Without the double quoting, it's:

    token 1 = "unrar"
    token 2 = "x"
    token 3 = "$f"


"unrar" is a command which can be executed (in this case with a parameter of
"x"), whereas "unrar x" is not a command.

You can see much the same thing if you try:

for f in one two three four
do
echo "$f"
done

for f in "one two" "three four"
do
echo "$f"
done


Antony.

--
The lottery is a tax for people who can't do maths.

                                                   Please reply to the list;
                                                         please *don't* CC me.