bug-bash
[Top][All Lists]
Advanced

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

Re: how does escaping in "`...`" work?


From: Eric Blake
Subject: Re: how does escaping in "`...`" work?
Date: Mon, 07 Jun 2010 17:20:03 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100430 Fedora/3.0.4-3.fc13 Lightning/1.0b2pre Mnenhy/0.8.2 Thunderbird/3.0.4

On 06/07/2010 04:56 PM, Bob Proulx wrote:
> Matthew Woehlke wrote:
>> How should bash interpret escapes in constructs like "`...`"?
> 
> The quoting rules for backticks are complex enough that the entire
> construct has long been replaced with a completely different one.  I
> strongly suggest that instead of struggling to get the backtick syntax
> quoted satisfactorily that you use $(...) instead.  The rules there
> are much more reasonable.

I agree with Bob that $() is much nicer than ``.  But if you care about
portability, such as for Solaris /bin/sh, then you are forced to use ``.
 And unfortunately, if you care about portability, then you must never
use embedded "" inside `` inside "", because not all shells follow the
POSIX rules on that construct.  POSIX requires that you use \" inside
"``", because it states that "`" has undefined behavior (some older
shells let the first unquoted " terminate the ``, even if there wasn't a
matching `).

It is always possible to write `` constructs that do not require ""
nesting, by using a temporary variable.  Besides, this also works around
existing buggy shells (thankfully not bash) that mishandle signals
arriving in the middle of ``, where the bad shells then proceed to
execute the outer command with an empty string in the place of the ``
instead of aborting the outer command.  That is:

echo "`echo \"a b\"`"

ends up as echo "" in some buggy shells, but:

tmp=`echo "a b"`
echo "$tmp"

will never execute the second echo if the first one was interrupted.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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