tags:

views:

215

answers:

3

Hello!

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`
+7  A: 

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 possible with the old syntax, but works fine with the new.

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

unwind
+5  A: 

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.

elliottcable
+4  A: 

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

Keltia
Interesting. I always thought that the $( ) syntax was a pure bash addition (a very nice one, 'though).
Joachim Sauer
I first saw it in ksh, back in 1992-or-so, before I'd used bash even once.
Vatine
It's commonly thought that $() is bash specific, but that's because vim doesn't syntax highlight as per POSIX, rather using the orig shell syntax that _very_ few people code in today. To tell vim to use sensible syntax highlighting for shell scripts put this in your .vimrc let g:is_posix = 1
pixelbeat
I think it did appear in ksh(1) first and adopted by POSIX afterwards.
Keltia