:: Re: [DNG] bash / quote weirdness
Startseite
Nachricht löschen
Nachricht beantworten
Autor: .
Datum:  
To: dng
Betreff: Re: [DNG] bash / quote weirdness

On Wed, 2022-01-12 at 00:08 +0100, Florian Zieboll via Dng wrote:
>> Dear list,
>>
>> this im my 'test.sh':
>>
>> #!/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

>>
>> ???
>>
>> Commands without parameters resp. whitespace (e.g. xcmd="unzip") work
>> fine when double-quoted; a web search (including the "GNU Bash
>> manual"
>> [1]) did not shed any light on this mystery...
>>
>> Thank you and libre Grüße,
>> Florian
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Thu, 13 Jan 2022 09:07:22 -0500
>> From: Hendrik Boom <hendrik@???>
>> To: dng@???
>> Subject: Re: [DNG] [OT] bash / quote weirdness
>> Message-ID: <20220113140722.GA30938@???>
>> Content-Type: text/plain; charset=us-ascii
>>
>> On Wed, Jan 12, 2022 at 05:45:08PM -0500, Steve Litt wrote:
>> On the other hand...
>>
>> =======================================
>> [slitt@mydesk ~]$ cat -n /etc/fstab | cut -b 1-20 |  head -n5
>>       1    UUID=730eaf92
>>       2    UUID=41abb5fd
>>       3    UUID=96cfdfb3
>>       4    UUID=6F66-BF7
>>       5    tmpfs /tmp tm
>> [slitt@mydesk ~]$ "cat -n" /etc/fstab | cut -b 1-20 |  head -n5
>> bash: cat -n: command not found
>> [slitt@mydesk ~]$ "cat -n /etc/fstab" | cut -b 1-20 |  head -n5
>> bash: cat -n /etc/fstab: No such file or directory
>> [slitt@mydesk ~]$
> So if it has parameters it's a command, and if it diesn't it's just
> a file or directory?

>
> -- hendrik
>
>
> ------------------------------
>
> Message: 3
> Date: Thu, 13 Jan 2022 15:43:29 +0100
> From: Antony Stone <Antony.Stone@???>
> To: dng@???
> Subject: Re: [DNG] [OT] bash / quote weirdness
> Message-ID: <202201131543.29980.Antony.Stone@???>
> Content-Type: Text/Plain; charset="utf-8"
>
> On Thursday 13 January 2022 at 15:07:22, Hendrik Boom wrote:
>
>> On Wed, Jan 12, 2022 at 05:45:08PM -0500, Steve Litt wrote:
>>> [slitt@mydesk ~]$ cat -n /etc/fstab | cut -b 1-20 | head -n5
>>>
>>>       1    UUID=730eaf92
>>>       2    UUID=41abb5fd
>>>       3    UUID=96cfdfb3
>>>       4    UUID=6F66-BF7
>>>       5    tmpfs /tmp tm

>>>
>>> [slitt@mydesk ~]$ "cat -n" /etc/fstab | cut -b 1-20 | head -n5
>>> bash: cat -n: command not found
>>>
>>> [slitt@mydesk ~]$ "cat -n /etc/fstab" | cut -b 1-20 | head -n5
>>> bash: cat -n /etc/fstab: No such file or directory
>> So if it has parameters it's a command, and if it diesn't it's just
>> a file or directory?
> It looks a good deal more complicated than that...
>
> $ "cat /etc/fstab"
> bash: cat /etc/fstab: No such file or directory
>
> $ "cat fstab"
> bash: cat fstab: command not found
>
> I have no idea what's really going on here.
>
>
> Antony.


The shell receives a series of tokens, and tries to interpret the first
one as a command.  In the double-quoted attempt above, it gets two
tokens before the first pipe | ---

    1) "cat -n"

    2) /etc/fstab

Of course, the system has no command named "cat -n".  (And only a
chaotic evil person would use a space in a command's name.) Something like
    "cat"  "-n"  /etc/fstab
would work fine, the shell now sees three tokens (and the double quotes
are completely unnecessary here), and the first is recognized as a
command that's on the executable path.

The same goes for "cat /etc/fstab" or "cat fstab", they're both just
text strings that happen to include a space character.