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. :)
...
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 hidrm -rf *
as an arg to a complex command line in an answer. – Paul_Pedant Oct 01 '20 at 11:34stuff
? also tackle the issue. – Quasímodo Oct 01 '20 at 14:33$(
requires pressing Shift+4 and then Shift+8. – Clashsoft Oct 03 '20 at 11:08