290

There are two syntaxes for command substitution: with dollar-parentheses and with backticks. 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?

tshepang
  • 65,642

6 Answers6

399

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 :)

wag
  • 35,944
  • 12
  • 67
  • 51
  • 12
    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
  • @Kendall: and here I thought '{' was the left bracket for all those years... – SamB Jan 13 '11 at 20:32
  • 7
    @Sam: { } is usually called "curly brackets" or "braces" http://en.wikipedia.org/wiki/Braces_(punctuation)#Braces – Jørn Schou-Rode Jan 13 '11 at 21:35
  • 2
    I also refer to '{' as curly braces. Though it seems odd you need to add the qualifier "curly" if you call the other things brackets... I guess it's just because they actually curl. – Kendall Helmstetter Gelner Jan 14 '11 at 04:23
  • @Jefromi I was going to say, backticks are easy to type on a UK keyboard. But then I checked where they are on a US keyboard, and it's the same. Top left with no shift. So I don't get it. – slim Jan 14 '11 at 10:53
  • @slim: I meant typing so many within the SE engine's inline code blocks. – Cascabel Jan 14 '11 at 15:13
  • @Kendall, i think this would be a question for english.SE, anyway to avoid further confusion lets refer to them as ( ). – wag Jan 14 '11 at 21:20
  • How about we just call them Curlers (assuming you meant curly braces in that weird paren set you had in your comment)? – Kendall Helmstetter Gelner Jan 15 '11 at 02:56
  • 1
    @slim I don't know on US/UK keyboards, but on spanish keyboards ``` is a dead key, so I have to type either a double-backtick (something I usually forget I can even do) or backtick then space, which is a pain. – Darkhogg Apr 04 '14 at 16:39
  • Can you explain why the escaped backticks work? How does bash process that line? – sbhatla Apr 25 '16 at 22:44
  • @KendallHelmstetterGelner, I call {} "curly braces" and I call [] "square brackets." Yes, it's redundant, but it eliminates any possibility of confusion. High entropy in English just adds potential of misunderstandings. – Wildcard Aug 15 '16 at 21:13
  • @sbhatla the escaping works because it stops them being interpreted by the outer layer. The escape is however striped off, so interpreted at the next layer. I had forgotten how complex this can get. It is much easier with $(). – ctrl-alt-delor Jun 15 '19 at 15:18
  • @slim to it is not a dead key on UK/US (as we don't have accents). The only dead key on my keyboard in the key to the right of the left-shift; It is the compose key, so I can do «compose» : ) ☺or accents «compose» a ' á … (My keyboard layout is Dvorak, with no £ or ¬, as I loose a key for the compose-key. – ctrl-alt-delor Jun 15 '19 at 15:24
40

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.

tshepang
  • 65,642
balki
  • 4,407
  • 10
    Some Bourne-derived shells don't recognize source. Dash is one example. – Dennis Williamson Jan 13 '11 at 16:40
  • 15
    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 – Stéphane Chazelas Oct 25 '12 at 10:17
  • 3
    Correction, only in (t)csh can they not be nested. (t)csh don't support $(...) though. They do support source (and not .) though. – Stéphane Chazelas Aug 01 '14 at 12:28
  • 1
    "you cannot nest backticks" is only accurate if we imply "without any other addition or modification, i.e. without adding any escape characters". But I guess (also based on the downvotes and on the previous, upvoted comments) this is not how most people read it. A clarifying "without adding proper escaping at each nesting level" would significantly improve this answer's clarity. – fra-san Oct 01 '20 at 11:59
31

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

mouviciel
  • 1,235
11

To add to what others said here, you can use the backticks to simulate inline comments:

echo foo `# I'm a comment!` bar

The output is: foo bar.

See the following for more information: https://stackoverflow.com/a/12797512 (Note also the comments below that post.)

phk
  • 5,953
  • 7
  • 42
  • 71
6

Another note, $() will use more system resource than using backticks, but is slightly faster.

In Mastering Unix shell scripting, Randal K. Michael had done a test in a chapter named "24 Ways to Process a File Line-by-Line".

tshepang
  • 65,642
cuonglm
  • 153,898
  • 2
    This claim is nonsense. There is no reason why it should be faster as it is just using a different notation for the parser. – schily Sep 06 '15 at 13:17
  • @schily: Maybe, I only quote from the book, you can read it for more details. – cuonglm Sep 06 '15 at 15:19
  • 3
    I would tend to agree with @schily...why would it take more resources? – Wildcard Mar 07 '16 at 23:21
  • 2
    @Wildcard, I suppose it's because $() makes your script one byte bigger than if it used ``` (assuming you don't nest them and don't use backslashes within). As to which would be faster to parse, that would vary between shells and would be irrelevant as negligible compared to the cost of creating a pipe and forking of process which command substitution entails. – Stéphane Chazelas Dec 02 '16 at 09:45
2

The $() syntax will not work with the old bourne shell.
With newer shells ` ` and $() are equivalent but $() is much more convenient to use when you need to nest multiple commands.

For instance :

echo $(basename $(dirname $(dirname /var/adm/sw/save )))

is easier to type and debug than :

echo `basename \`dirname \\\`dirname /var/adm/sw/save \\\`\``
Axel Beckert
  • 287
  • 1
  • 12
Emmanuel
  • 4,187
  • 1
    While $() may look nice, it is a pain when implementing a related parser because it requires a dual recursive parser. – schily Sep 06 '15 at 13:18
  • 7
    @schily On the other side, what would be a shell without a good parser. – Emmanuel Sep 08 '15 at 15:58
  • 1
    The problem is that you need to know where the string ends before you call the parser. This is relatively simple with the backticks, but it is hard with brackets as they are used for various purposes in the shell. So you need the parser twice and in a way that does not exist in the Bourne Shell. – schily Sep 08 '15 at 20:04