:: Re: [DNG] [OT] bash / quote weirdne…
Página Principal
Delete this message
Reply to this message
Autor: Dr. Nikolaus Klepp
Data:  
Para: dng
Assunto: Re: [DNG] [OT] bash / quote weirdness
Anno domini 2022 Thu, 13 Jan 15:43:29 +0100
Antony Stone scripsit:
> 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.


Your example misses a minor detail. "" and '' build strings with/without variable substitution (e.g. A="cat /etc/fstab"). When passed as a not-quoted variable (e.g. $A) to the current shell the whole string is broken up into arguments at whitespaces (e.g. $A -> "cat" "/etc/fstab"), the first argument is the command that gets passed all remaining arguments including pipe symbols ('|'). A quoted variable is passed as one argument ("$A" -> "car /etc/fstab") - if it's the only argument then that programm/command/function is evaluated and is most likely to fail. Note that the pipe symbol ("|") as part of a string is passed as an argument or part of an argument to argument 0 (command) and does not build a pipe.

Nik


>
>
> Antony.
>




--
Please do not email me anything that you are not comfortable also sharing with the NSA, CIA ...