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/28/2002 09:20:51
> > 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>
Interestingly I'm testing with the only character that differs between the
two sets! The ordering of these two clauses ought to be reversed...
> 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:
Yes, that is how I read it. I've lost the section (again), 'implementation
defined' might include 'format hard disk' :-)
> > 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 $().
As you noted $() is different.
Having sorted out "`...`", time to consider ${x:-word} and ${x%word}.
or more particularly "${x:-"word"}" and "${x%"word"}".
I think the statement "Enclosing the full parameter expansion string in
double-quotes shall not cause the following four varieties of pattern
characters to be quoted, whereas quoting characters within the braces shall
have this effect." just before the definition of % %% # and ## means
that in "${x%"word"}" the quotes around 'word' are necessary in order
to quote it. The position of the statement implies that they are
unnecessary for :- so 'word' is already quoted in "${x:-word}".
So it is plausible that in "${x:-"word"}" word is actually unquoted!
(Of course "" have little effect on 'word' in the :- case. Mainly
changing the effect of \ and ').
I think the intention is to preserve the traditional (ie AT&T sh)
behaviour for :- (and friends), but to require quoting for %.
These two examples are in the document:
"${x#*}"
The asterisk is a pattern character.
${x#"*"}
The literal asterisk is quoted and not special.
but not "${x#"*"}" and there are no examples of :- with quotes.
David
--
David Laight: david@l8s.co.uk