[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Avoid unportable nested double-quotes and backquotes.

From: Ralf Wildenhues
Subject: Re: Avoid unportable nested double-quotes and backquotes.
Date: Tue, 22 Jun 2010 19:56:40 +0200
User-agent: Mutt/1.5.20 (2010-04-22)

Hello Bruno,

picking up on a slightly older thread:

* Bruno Haible wrote on Sun, May 23, 2010 at 01:37:41PM CEST:
> > noticed this small shell unportability in gnulib coming from gettext.
> > ...
> >          ac_dir=`echo "$ac_file"|sed 's%/[^/][^/]*$%%'`
> > -        ac_dir_suffix="/`echo "$ac_dir"|sed 's%^\./%%'`"
> > +        ac_dir_suffix=/`echo "$ac_dir"|sed 's%^\./%%'`
> There is no unportability here. I tested these statements
>    ac_dir=./foo
>    ac_dir_suffix="/`echo "$ac_dir"|sed 's%^\./%%'`"
>    echo "$ac_dir_suffix"
>    ac_dir=b/foo
>    ac_dir_suffix="/`echo "$ac_dir"|sed 's%^\./%%'`"
>    echo "$ac_dir_suffix"
> and got the same result in bash, ksh, zsh, dash, /bin/sh on Solaris
> and other systems, and pdksh 5.2.14 (= /bin/sh on OpenBSD 4.5) (without
> and with 'set -o posix').

With the very expressions you are using, you are getting lucky to not
hit the bug.  If you just add a space before $ac_dir, then all of a
sudden Solaris sh and ksh and other old shells fall apart quickly:

$ ac_dir=./foo
$ ac_dir_suffix="/`echo " $ac_dir"|sed 's%^\./%%'`"
ksh: : cannot execute
ksh: ./foo|sed 's%^\./%%': cannot execute

So, this issue:

> I think you must be referring to
> <http://www.gnu.org/software/autoconf/manual/html_node/Shell-Substitutions.html>
> It says
>   "not all shells properly understand "`..."..."...`". Worse yet, not all
>    shells understand "`...\"...\"...`" the same way. There is just no portable
>    way to use double-quoted strings inside double-quoted back-quoted
>    expressions"
> but it does not go into details about which shells are affected.

really happens with sh and ksh on Solaris 10, HP-UX 11.31, AIX 5.3, IRIX
6.5, Tru64/OSF 5.1, and in a particularly ugly way, namely by trying to
execute some weirdly named programs.

> In summary, I did find a shell that does not understand "`...\"...\"...`"
> but I did not find a shell that does not understand "`..."..."...`". That
> paragraph in autoconf.texi dates back to 2000-05-19 12:17:29. It may have
> referred to shells that were in common use between 1995 and 2000 and have
> disappeared by now.

No, it is very much relevant until now.  I understand your particular
code by chance doesn't trigger the issue, but please do code reviewers
a favor by not making them stumble over such an almost-bug; removing the
outer double-quotes is perfectly portable on the RHS of an assignment,
but makes triggering the bug from a code edit much less likely.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]