Subject: Re: shell quoting problems
To: David Laight <david@l8s.co.uk>
From: Greg A. Woods <woods@weird.com>
List: tech-userlevel
Date: 11/27/2002 17:43:00
[ On Wednesday, November 27, 2002 at 11:34:09 (+0000), David Laight wrote: ]
> Subject: Re: shell quoting problems
>
> 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.

Yes, that's right.  By the time the inner echo is run the outer echo's
command-line parameters have already been created -- i.e. the quote
removal for the outer command has already happened -- so the
double-quote produced by the inner echo is just a string passed as a
parameter to the outer echo.


> However enclose it in "":
> 	echo "`echo \"`"
> And a different section seems to be in force (Double-Quotes) which
> says 'having escape characters removed'.

Yes, I believe that's right....

> Is this 'all escapes', or just those that escape $, ` or \ ?

It is exactly those characters defined in the very next point:

    \
           The backslash shall retain its special meaning as an escape
           character (see Escape Character (Backslash)) only when
           followed by one of the following characters when considered
           special:
                                                                                
                          $   `   "   \   <newline>                                                      
                                                                                
I.e. the command to be executed for the substitution becomes:

	echo "

which traditionally causes a blank line to be printed, though in POSIX
I believe the actual result is implementation defined:

    Exit Status and Errors

    Consequences of Shell Errors

   For a non-interactive shell, an error condition encountered by a
   special built-in (see Special Built-In Utilities ) or other type of
   utility shall cause the shell to write a diagnostic message to
   standard error and exit as shown in the following table:

[[....]]

   An expansion error is one that occurs when the shell expansions
   defined in Word Expansions are carried out (for example, "${x!y}" ,
   because '!' is not a valid operator); an implementation may treat
   these as syntax errors if it is able to detect them during
   tokenization, rather than during expansion.

   If any of the errors shown as "shall exit" or "(may) exit" occur in a
   subshell, the subshell shall (respectively may) exit with a non-zero
   status, but the script containing the subshell shall not exit because
   of the error.

   In all of the cases shown in the table, an interactive shell shall
   write a diagnostic message to standard error without exiting.


> Maybe the \ should be removed on the initial parsing.  So:
> 	echo "`echo \\\"`"
> would be needed to get " output.

Yes, I think I would agree with that.

Interestingly KSH Version M-11/16/88i does that correctly with
back-quotes (as it seems does SysVr4 /bin/sh) but not with $().

-- 
								Greg A. Woods

+1 416 218-0098;            <g.a.woods@ieee.org>;           <woods@robohack.ca>
Planix, Inc. <woods@planix.com>; VE3TCP; Secrets of the Weird <woods@weird.com>