Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Running top -p $(pidof init) and top -p `pidof init` gives the same output. Are these two ways of doing the same thing, or are there differences?

share|improve this question
11  
See also: BashFAQ/082. – Dennis Williamson Jan 13 '11 at 16:38
19  
For a second there I thought this was a jQuery question. – David Murdoch Jan 13 '11 at 20:42
The result might depend on the shell - some support both. – artdanil Feb 15 '11 at 22:33

3 Answers

up vote 163 down vote accepted

The old-style backquotes ` ` do treat backslashes and nesting a bit different. The new-style $() interprets everything in between ( ) as a command.

echo $(uname | $(echo cat))
Linux

echo `uname | `echo cat``
bash: command substitution: line 2: syntax error: unexpected end of file
echo cat

works if the nested backquotes are escaped:

echo `uname | \`echo cat\``
Linux

backslash fun:

echo $(echo '\\')
\\

echo `echo '\\'`
\

The new-style $() applies to all POSIX-conformant shells.
As mouviciel pointed out, old-style ` ` might be necessary for older shells.

Apart from the technical point of view, the old-style ` ` has also a visual disadvantage:

  • Hard to notice: I like $(program) better than `program`
  • Easily confused with a single quote: '`'`''`''`'`''`'
  • Not so easy to type (maybe not even on the standard layout of the keyboard)

(and SE uses ` ` for own purpose, it was a pain writing this answer :)

share|improve this answer
4  
The only thing I would add, is that I call '(' a paren, not a bracket (which is '['). – Kendall Helmstetter Gelner Jan 13 '11 at 18:09
33  
You deserve all kinds of upvotes simply for managing to type so many backticks. – Jefromi Jan 13 '11 at 18:39
@Jefromi: ...and, he got them. :) – Jander Jan 13 '11 at 20:16
@Kendall: and here I thought '{' was the left bracket for all those years... – SamB Jan 13 '11 at 20:32
@Sam: { } is usually called "curly brackets" or "braces" en.wikipedia.org/wiki/Braces_(punctuation)#Braces – Jørn Schou-Rode Jan 13 '11 at 21:35
show 5 more comments

Obvious difference I observe is that you cannot nest backticks while you can nest $(). Maybe both exist for legacy reasons. Similarly, the . and source commands are synonyms.

share|improve this answer
6  
Some Bourne-derived shells don't recognize source. Dash is one example. – Dennis Williamson Jan 13 '11 at 16:40
2  
That's not true. You can nest backtick to any level, just more painfully. Note that both $(...) and `...` are standard (the latter being deprecated) while . is standard but not source – Stephane Chazelas Oct 25 '12 at 10:17

$() does not work with old Bourne shell. But it has been years since I worked with old Bourne shell.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.