What is the difference between $(command) and `command` in shell programming?

后端 未结 6 1745
误落风尘
误落风尘 2020-11-22 01:15

To store the output of a command as a variable in sh/ksh/bash, you can do either

var=$(command)

or

var=`command`

6条回答
  •  爱一瞬间的悲伤
    2020-11-22 01:44

    The backticks/gravemarks have been deprecated in favor of $() for command substitution because $() can easily nest within itself as in $(echo foo$(echo bar)). There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc.

    See BashFAQ/082 for several reasons to always prefer the $(...) syntax.

    Also see the POSIX spec for detailed information on the various differences.

提交回复
热议问题