25

Since being corrected many years ago, I switched from backticks to $() for command expansion.

But I still prefer the backticks. It is fewer keystrokes and does not involve the Shift key.

I understand that the parentheses are preferable because it is less prone to the errors that backticks is prone to, but what is the reason for the rule to never use backticks?

Stephen Boston
  • 2,178
  • 4
  • 32
  • 55
  • 6
    See also How to use a special character as a normal one?. backticks can only be used portably and reliably in scalar variable assignments. – Stéphane Chazelas Oct 01 '20 at 10:10
  • 6
    Apart from technical issues: I just got fed up with posting solutions with ... where people just put them in as single quotes or double quotes or UTF-8 apostrophes, and then badmouthed me for posting solutions that didn't work when I knew I had tested them. $( .. ) is a visual trigger when you are debugging, where you might not eyeball back-ticks. I even saw a malicious post on LinixMint forum where somebody hid rm -rf * as an arg to a complex command line in an answer. – Paul_Pedant Oct 01 '20 at 11:34
  • Personally, I started using $() when I realized that typing $(!!) involved much less twiddling the shift key than `!!`. That, and the occasional nested usage. – stolenmoment Oct 01 '20 at 13:21
  • 6
    There's no such "rule" to never use backticks. But avoid using backticks when posting on unix.se, unless you deliberately want to trigger some people. –  Oct 01 '20 at 16:18
  • 3
    I prefer using $() over backticks because it makes the code look cleaner to me. – Peschke Oct 01 '20 at 17:19
  • 3
    The main reason to not use backticks in the code is that they are for code highlighting here. – Paŭlo Ebermann Oct 02 '20 at 03:32
  • @PaŭloEbermann I agree that in scripts the parens is much preferred, but I'm thinking of the case of the quick one-liner, on the fly. I should have made that more clear in the question and would add it in now, but there have been many interesting comments to my unclear question so... – Stephen Boston Oct 02 '20 at 13:37
  • 1
    To give some perspective on "It is fewer keystrokes and does not involve the shift key.": Using a German Windows keyboard with a Mac, to type a backtick I have to press Shift+´, and then additionally press the spacebar to cancel the creation of a grave accented character like à. To be fair, typing $( requires pressing Shift+4 and then Shift+8. – Clashsoft Oct 03 '20 at 11:08
  • 2
    Meta: Backticks in comments here...: Escape a backtick by a backslash: \` – Peter Mortensen Oct 03 '20 at 20:41

2 Answers2

48

The Bash FAQ gives a number of reasons to prefer parentheses to backticks, but there isn’t a universal rule that you shouldn’t ever use backticks.

The main reason to prefer parentheses in my view is that parsing inside $() is consistent with parsing performed outside, which isn’t the case with backticks. This means that you can take a shell command and wrap it with "$()" without much thought; that’s not true if you use backticks instead. This cascades, so wrapping a command which itself contains a substitution is easily done with "$()", not so with backticks.

Ultimately I think it’s a question of habit. If you choose to use backticks for simple cases, parentheses for others, you’ll have to make that choice every time you want to substitute a command. If you choose to always use parentheses, you never have to think about it again.

The latter can explain the presence of a “don’t use backticks” rule in certain coding guides: it simplifies development, and removes a source of errors for developers and reviewers. It also explains why using parentheses can be recommended even for one-liners: it’s hard to ingrain a habit for script-writing when it’s not applied everywhere.

(As far as keying goes, that depends on the keyboard layout; on my AZERTY keyboard, $() doesn’t involve any shifting, whereas backticks are quite painful to write.)

Stephen Kitt
  • 434,908
20

Personally, I would use $(...), always, just because it's more consistent in the corner cases of nested quotes and expansions, and I like the idea of having expansions start consistently with a dollar sign, and the parenthesis are more visible than backticks which are rather light in some fonts, and can get confused with single quotes like mentioned in the comments. But other than the first, those are about looks and aesthetics, so your opinion may well differ.

Based on the discussion in Have backticks (i.e. `cmd`) in *sh shells been deprecated?, it doesn't seem that support for backticks would be on track to be removed, so that's not a reason to say "never".

However, note that the parsing of backticks works oddly in some cases. Having to add extra backticks for nested expansions is rather simple:

echo `echo \`echo foo\` `

But that's not all. As Stéphane notes in an answer to How to use a special character as a normal one?, even nesting double quotes with backticks in the between fails in ksh, for example (I'll reproduce this one here for easier access):

ksh$ echo "`date +"%F %T"`"  
ksh: : cannot execute [Is a directory]
2020-10-01 %T

and you have to escape the inner double quotes too, even though this looks like it would be unambiguous to parse even without them:

ksh$ echo "`date +\"%F %T\"`"  
2020-10-01 14:14:27

(The quotes around the command substitution are relevant because of command substitution, though not with echo here.)

So, while this does seem like a case of "never say never", and while backticks work fine in the simple cases, I think there's enough to strongly suggest just using $() instead. :)

ilkkachu
  • 138,973
  • Simplifying nested expressions is very important. Also huge: avoiding backslashes as much as possible, by pretty much whatever means necessary. Escaping metacharacters and escaping escape characters is a great recipe for confusion, unreadable and incomprehensible code, and bugs including security holes. – jrw32982 Oct 07 '20 at 18:48