Bash script variables question[Scanned]

Garry T. Williams gtwilliams at gmail.com
Sun Aug 6 18:54:27 UTC 2006


On Sunday 06 August 2006 14:31, Paul Howarth wrote:
> On Sun, 2006-08-06 at 13:27 -0400, Tom Diehl wrote:
> > Any idea why the authors feel the need to break 20+ years of
> > history? I fail to see why foo=`command` is better or worse than
> > foo=$(command).

[snip]

> It allows constructs like this:
> 
> $ var=$(foo $(bar))

You mean like this?

    var=`foo \`bar\``

There are restrictions, but that isn't one of them.

    $ sh -c 'var=`echo \`ls .zshrc\``;echo $var'
    .zshrc

The Korn shell manual page does document the restrictions:

     Additionally, the backquoted syntax has historical  restric-
     tions on the contents of the embedded command. While the new
     $() 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 <<eeof                  | cat <<eeof
     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.

-- 
Garry T. Williams --- +1 678 656-4579




More information about the fedora-list mailing list