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

BashShellKshSh

Bash Problem Overview


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

var=$(command)

or

var=`command`

What's the difference if any between the two methods?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

They behave the same. The difference is syntactical: it's easier to nest $() than ``:

listing=$(ls -l $(cat filenames.txt))

vs.

listing=`ls -l \`cat filenames.txt\``

Solution 3 - Bash

July 2014: The [commit f25f5e6][1] (by [Elia Pinto (devzero2000)][2], April 2014, Git 2.0) adds to the nesting issue:

> The backquoted form is the traditional method for command substitution, and is supported by POSIX.
However, all but the simplest uses become complicated quickly.
In particular, embedded command substitutions and/or the use of double quotes require careful escaping with the backslash character
.

That is why the [git/Documentation/CodingGuidelines ][3] mentions:

> We prefer $( ... ) for command substitution; unlike ``, it properly nests.
It should have been the way Bourne spelled it from day one, but unfortunately isn't.

[thiton][4] [commented][5]:

> That is why `echo `foo`` won't work in general because of the inherent ambiguity because each ``can be opening or closing.
It might work for special cases due to luck or special features.


Update January 2016: Git 2.8 (March 2016) gets rid of backticks entirely.

See commit ec1b763, commit 9c10377, commit c7b793a, commit 80a6b3f, commit 9375dcf, commit e74ef60, commit 27fe43e, commit 2525c51, commit becd67f, commit a5c98ac, commit 8c311f9, commit 57da049, commit 1d9e86f, commit 78ba28d, commit efa639f, commit 1be2fa0, commit 38e9476, commit 8823d2f, commit 32858a0, commit cd914d8 (12 Jan 2016) by Elia Pinto (devzero2000).
(Merged by Junio C Hamano -- gitster -- in commit e572fef, 22 Jan 2016)

From Git 2.8 onwards, it is all $(...), no more `...`. [1]: https://github.com/git/git/commit/f25f5e61a7c1a22dd85413d61db639e7c01268f4 [2]: https://github.com/devzero2000 [3]: https://github.com/git/git/blob/6f92e5ff3cdc813de8ef5327fd4bad492fb7d6c9/Documentation/CodingGuidelines#L72-L74 [4]: https://stackoverflow.com/users/847601/thiton [5]: https://stackoverflow.com/questions/8941381/what-is-the-difference-between-backticks-and-in-bash-script#comment11202254_8941461

Solution 4 - Bash

When the older back-tick form is used, backslash retains its literal meaning except when followed by $, `, or \. The first back-tick not preceded by a backslash terminates the command substitution.

When using the newer $(command) form, all characters between the parentheses make up the command; none are treated specially.

Both forms can be nested, but the back-tick variety requires the following form.

`echo \`foo\`` 

As opposed to:

$(echo $(foo))

Solution 5 - Bash

There is little difference, except for what unescaped characters you can use inside of the command. You can even put `...` commands inside $(...) ones (and vice versa) for a more complicated two-level-deep command substitution.

There is a slightly different interpretation of the backslash character/operator. Among other things, when nesting `...` substitution commands, you must escape the inner ` characters with \, whereas with $() substition it understands the nesting automatically.

Solution 6 - Bash

"What's the difference if any between the two methods?"

Make attention to this behaviour:

A="A_VARIABLE"
echo "$(echo "\$A")"
echo "`echo "\$A"`"

You will get these results:

$A
A_VARIABLE

 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhhafezView Question on Stackoverflow
Solution 1 - BashSiegeXView Answer on Stackoverflow
Solution 2 - BashJohn KugelmanView Answer on Stackoverflow
Solution 3 - BashVonCView Answer on Stackoverflow
Solution 4 - BashocodoView Answer on Stackoverflow
Solution 5 - BashDigitalRossView Answer on Stackoverflow
Solution 6 - BashHiroView Answer on Stackoverflow