The major one is the ability to nest them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks.
An example, though somewhat contrived:
deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)
which will give you a list of all files in the /dir directory tree which have the same name as the earliest dated text file from December 2011 (a).
Another example would be something like getting the name (not the full path) of the parent directory:
pax> cd /home/pax/xyzzy/plugh
pax> parent=$(basename $(dirname $PWD))
pax> echo $parent
xyzzy
(a) Now that specific command may not actually work, I haven't tested the functionality. So, if you vote me down for it, you've lost sight of the intent :-) It's meant just as an illustration as to how you can nest, not as a bug-free production-ready snippet.
Answer from paxdiablo on Stack OverflowGreg's Wiki
BashFAQ/082 - Greg's Wiki
The function of $(...) as being an expansion is visually clear. The syntax of a $-prefixed token is consistent with all other expansions that are parsed from within double-quotes, at the same time, from left-to-right. Backticks are the only exception.
Red Hat
Bash scripting: Moving from backtick operator to $ parentheses
January 12, 2023 - You can achieve the same result by replacing the backticks with the $ parens, like in the example below: ❯ echo "There are $(ls | wc -l) files in this directory" There are 3 files in this directory · Here's another example, still very simple but a little more realistic. I need to troubleshoot something in my network connections, so I decide to show my total and waiting connections minute by minute. ❯ cat netinfo.sh #!/bin/bash while true do ss -an > netinfo.txt connections_total=$(cat netinfo.txt | wc -l) connections_waiting=$(grep WAIT netinfo.txt | wc -l) printf "$(date +%R) - Total=m Waiting=m\n" $connections_total $connections_waiting sleep 60 done ❯ ./netinfo.sh 22:59 - Total= 2930 Waiting= 977 23:00 - Total= 2923 Waiting= 963 23:01 - Total= 2346 Waiting= 397 23:02 - Total= 2497 Waiting= 541
LinuxOPsys
Bash Backticks vs Dollar Parentheses $()
March 28, 2023 - Within backticks the backslash quotes are evaluated before other kinds of internal quotes are applied. $() operators are much more convenient to use when you need to nest multiple commands.
LinuxQuestions.org
Bash: When to use backtick and when to use $().
I read in some documentation and bash script comments that it is proper to use () for program output rather than the backticks ``. For example, to be.
Linux.org
backtick usage | Linux.org
December 12, 2023 - So you can use backticks to put the result of a command, into a variable. Example: my_machine=`uname -n` Many years before now - 2023 - I learned that you can do the same with another syntax. This one being: my_machine=$(uname -n) I would prefer the second one because it doesn't use any...
Linux Hint
Mastering Backticks in Linux Bash Scripts
It runs the commands in the system and returns the output to continue the logic in the particular script. In simple words, the backticks in Bash scripts work as a bridge between two commands, which means that the second command’s action depends upon the first one.
Unix Community
Why $() is preferred instead of backticks in Bash? - Shell Programming and Scripting - Unix Linux Community
June 24, 2022 - Environment: Bash in Linux distributions like RHEL, Oracle Linux Question from a beginner. Is it true that for variable assignments in bash shell, it is better to use $ with round brackets (parentheses) as shown in method 2 below rather using backticks shown in method 1 below ? If so, why ?
FreeBSD
Shell - backticks within backticks | The FreeBSD Forums
August 12, 2018 - Keep even one-liners readable and easy to follow (and/or comment them!). I'm usually using the $() notation at the very end/beginning of longer pipes, as (IMHO) they kind of resemble the punctiation for code blocks known from other (script) languages. Backticks - especially with some fonts and on smaller displays - are not that obvious and make the block hard to distinguish.
The Geek Diary
Backtick (`) symbol in Linux Shell Scripting – The Geek Diary
The shell runs the command within the backticks and assigns the output to the variable testing. Here’s an example of creating a variable using the output from a normal shell command: $ cat myscript.sh #!/bin/bash # using the backtick character testing=`date` echo "The date and time are: $testing" $