开发者

Is command substitution $(foo) bashism?

开发者 https://www.devze.com 2023-01-17 17:24 出处:网络
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.

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 s开发者_开发问答h 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).


Actually, the $(...) command substitution syntax is defined by POSIX, though it's 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.


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 :

  • man dash
  • The Open Group Base Specifications - Shell Command Language thanks to @Ned Deily for this good resource :)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号