Karl Yngve Lervåg
Karl Yngve Lervåg

Reputation: 1715

What's the difference between $(...) and `...`

The question is as simple as stated in the title: What's the difference between the following two expressions?

$(...)
`...`

For example, are the two variables test1 and test2 different?

test1=$(ls)
test2=`ls`

Upvotes: 10

Views: 730

Answers (3)

unwind
unwind

Reputation: 400109

The result is the same, but the newer $() syntax is far clearer and easier to read. At least doubly so when trying to nest. Nesting is not easy with the old syntax, but works fine with the new.

Compare:

$ echo $(ls $(pwd))

versus:

$ echo `ls \`pwd\``

You need to escape the embedded backticks, so it's quite a lot more complicated to both type and read.

According to this page, there is at least one minor difference in how they treat embedded double backslashes.

Upvotes: 9

Keltia
Keltia

Reputation: 14743

Using ``` is the historical syntax, POSIX has adopted the now standard ` $(...) syntax. See Section 2.6.3

Upvotes: 5

ELLIOTTCABLE
ELLIOTTCABLE

Reputation: 18108

You might want to read man bash:

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or . The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

That's under the "Command Substitution" section of the manpage.

Upvotes: 5

Related Questions