bug-bash
[Top][All Lists]
Advanced

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

Re: how does bash parse back-ticks, anyway?


From: Eric Blake
Subject: Re: how does bash parse back-ticks, anyway?
Date: Mon, 02 Apr 2007 20:11:19 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.4.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Matthew Woehlke on 4/2/2007 5:09 PM:
>> Matthew, it's not a bug in bash, it's a feature. $(foo) and `foo`
>> are not 100% equivalent. Read the command substitution paragraph
>> in the bash man page.
> 
> I did, and I saw that it talked about them being different, but I didn't
> understand *how* they were supposed to be different (besides, you've got
> some weirdnesses in there that look like they should/shouldn't work
> regardless of the back-ticks). In particular I fail to understand why
> whether or not there are quotes around the back-ticks should have any
> effect (which it clearly does). So until someone on bug-bash explains to
> me differently, I'm not convinced bash.xml is wrong.

Read the POSIX rationale:
http://www.opengroup.org/onlinepubs/009695399/xrat/xcu_chap02.html

| The "$()" form of command substitution solves a problem of inconsistent
| behavior when using backquotes. For example:
|
| Command             Output
| echo '\$x'          \$x
| echo `echo '\$x'`   $x
| echo $(echo '\$x')  \$x
|
| Additionally, the backquoted syntax has historical restrictions on the
| contents of the embedded command. While the newer "$()" form can process
| any kind of valid embedded script, the backquoted form cannot handle
| some valid scripts that include backquotes. For example, these otherwise
| valid embedded scripts do not work in the left column, but do work on
| the right:
|
|     echo `                         echo $(
|     cat <<\eof                     cat <<\eof
|     a here-doc with `              a here-doc with )
|     eof                            eof
|     `                              )
|
|     echo `                         echo $(
|     echo abc # a comment with `    echo abc # a comment with )
|     `                              )
|
|     echo `                         echo $(
|     echo '`'                       echo ')'
|     `                              )
|
| Because of these inconsistent behaviors, the backquoted variety of
| command substitution is not recommended for new applications that nest
| command substitutions or attempt to embed complex scripts.

> 
> If nothing else, I'll stand by it being a bash bug, if only in the docs
> because they are not sufficiently clear. :-)
> 

The POSIX rules here are that when parsing plain ``, \ quotes only $, `,
and \.  When surrounding `` inside "", the escape characters special to ""
are removed prior to handling ``, which means that you have to
additionally escape $, `, ", \, and newline to account for the added level
of "" parsing.  It is this additional level of escaping which applies only
to `` that makes "``" hard to parse, and which does not apply to $()
parsing.  And as the autoconf manual documents, there is no portable way
to include a double-quoted string inside a "``" sequence.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGEbfH84KuGfSFAYARAokDAJ90H2gp52xiZpxYUMKTCgL7uSeRigCcDpvB
m2Ly3pW4oFTE/B0AvihPcK4=
=4kbd
-----END PGP SIGNATURE-----




reply via email to

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