Subject: Re: shell quoting problems
To: NetBSD Userlevel Technical Discussion List <tech-userlevel@NetBSD.ORG>
From: David Laight <david@l8s.co.uk>
List: tech-userlevel
Date: 11/27/2002 11:34:09
I've been going slowly mad trying to make sense of this document
(the text available online from TOG is more or less identical).

Consider how to expand:
	echo `echo \"`
The description of `command` says that the \ shall keep its literal
meaning (since it isn't followed by $, ` or \).
So we execute echo \" and get a " output.

However enclose it in "":
	echo "`echo \"`"
And a different section seems to be in force (Double-Quotes) which
says 'having escape characters removed'.
Is this 'all escapes', or just those that escape $, ` or \ ?
Maybe the \ should be removed on the initial parsing.  So:
	echo "`echo \\\"`"
would be needed to get " output.
That works for netbsd sh, solaris 8 sh and solaris 8 ksh, however
netbsd ksh (pdksh) is broken here and complains 'no closing quote'.

With one less \:
	echo "`echo \\"`"
the netbsd sh and ksh both manage to output ", I've not worked out
how they parse it though.

Unfortunately I can't write a test case unless I know the correct
one...

> `command`  
>    
>    Within  the  backquoted style of command substitution, backslash shall
>    retain  its  literal  meaning, except when followed by: '$' , '`' , or
>    '\'  (dollar  sign, backquote, backslash).

>     Double-Quotes
>     
>    Enclosing  characters  in  double-quotes  (  ""  )  shall preserve the
>    literal  value  of  all  characters within the double-quotes, with the
>    exception  of the characters dollar sign, backquote, and backslash, as
>    follows:
...
>    `
>           The  backquote shall retain its special meaning introducing the
>           other   form   of   command   substitution   (see   [24]Command
>           Substitution  ).  The  portion  of  the  quoted string from the
>           initial  backquote  and the characters up to the next backquote
>           that  is  not preceded by a backslash, having escape characters
>           removed,  defines  that  command  whose output replaces "`...`"
>           when  the  word  is  expanded.

> IMNSHO the back-quote form of command-substitution should have been
> deprecated LONG ago!

You can't, because the default shell on many systems doesn't
support $(...).
No one dare change the default shell either, because some scripts
depend on the specific quoting rules of the actual shell.
These will be undocumented and baroque, reminds me of the
statement 'Although robust enough for general use, adventures into the
esoteric periphery of the C shell may reveal unexpected quirks.' on
some csh man pages.

	David

-- 
David Laight: david@l8s.co.uk