What's the difference between $(stuff) and `stuff`?

38,499

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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".

Share:
38,499

Related videos on Youtube

tshepang
Author by

tshepang

I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.

Updated on September 17, 2022

Comments

  • tshepang
    tshepang almost 2 years

    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?

    • Brian Rasmussen
      Brian Rasmussen over 13 years
      See also: BashFAQ/082.
    • David Murdoch
      David Murdoch over 13 years
      For a second there I thought this was a jQuery question.
  • Brian Rasmussen
    Brian Rasmussen over 13 years
    Some Bourne-derived shells don't recognize source. Dash is one example.
  • Kendall Helmstetter Gelner
    Kendall Helmstetter Gelner over 13 years
    The only thing I would add, is that I call '(' a paren, not a bracket (which is '[').
  • SamB
    SamB over 13 years
    @Kendall: and here I thought '{' was the left bracket for all those years...
  • Jørn Schou-Rode
    Jørn Schou-Rode over 13 years
    @Sam: { } is usually called "curly brackets" or "braces" en.wikipedia.org/wiki/Braces_(punctuation)#Braces
  • Kendall Helmstetter Gelner
    Kendall Helmstetter Gelner over 13 years
    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.
  • slim
    slim over 13 years
    @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.
  • Cascabel
    Cascabel over 13 years
    @slim: I meant typing so many within the SE engine's inline code blocks.
  • wag
    wag over 13 years
    @Kendall, i think this would be a question for english.SE, anyway to avoid further confusion lets refer to them as ( ).
  • Kendall Helmstetter Gelner
    Kendall Helmstetter Gelner over 13 years
    How about we just call them Curlers (assuming you meant curly braces in that weird paren set you had in your comment)?
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    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
  • Darkhogg
    Darkhogg about 10 years
    @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.
  • Stéphane Chazelas
    Stéphane Chazelas almost 10 years
    Correction, only in (t)csh can they not be nested. (t)csh don't support $(...) though. They do support source (and not .) though.
  • schily
    schily almost 9 years
    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
    schily almost 9 years
    While $() may look nice, it is a pain when implementing a related parser because it requires a dual recursive parser.
  • Wildcard
    Wildcard over 8 years
    I would tend to agree with @schily...why would it take more resources?
  • sbhatla
    sbhatla about 8 years
    Can you explain why the escaped backticks work? How does bash process that line?
  • Wildcard
    Wildcard almost 8 years
    @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.
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    @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.
  • ctrl-alt-delor
    ctrl-alt-delor about 5 years
    @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
    ctrl-alt-delor about 5 years
    @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.
  • Christopher
    Christopher almost 5 years
    Old as in 1970's and early 1980's, correct?
  • fra-san
    fra-san almost 4 years
    "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.