tags:

views:

50

answers:

2

there are two different syntaxes for command substitution

FOO=$(echo bar)

and

FOO=`echo bar`

as far as i know, the first method is defined in bash, while the second is defined in sh.

consider the following use of command substitution in an sh script

#!/bin/sh
FOO=$(echo bar)

does that fall under the definition of bashism?

bashisms, i.e. features not defined by POSIX (won't work in dash, or general /bin/sh).

+3  A: 

It's the same thing. So no it's not a bashism nor related to bash only.

Command Substitution
Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution occurs when the command is enclosed as follows:

$(command)

or ( ''backquoted'' version):

`command`

The shell expands the command substitution by executing command in a subshell environment and replacing the command substitution with the standard output of the command, removing sequences of one or more newlines at the end of the substitution. (Embedded newlines before the end of the output are not removed; however, during field splitting, they may be translated into spaces, depending on the value of IFS and quoting that is in effect.)


Resources :

Colin Hebert
Even more authoritative, here's a link to the Open Group Base / IEEE 1003 spec: http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_03
Ned Deily
+3  A: 

Actually, the $(...) command substitution syntax is defined by POSIX, though its not part of the earlier SVID sh standard. So as long as you don't care about running on pre-POSIX systems, it should be fine.

Chris Dodd