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 Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 9449778 › what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts
bash - What is the benefit of using $() instead of backticks in shell scripts? - Stack Overflow

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 stackoverflow.com
🌐
Stack Exchange
unix.stackexchange.com › questions › 27428 › what-does-backquote-backtick-mean-in-commands
shell - What does ` (backquote/backtick) mean in commands? - Unix & Linux Stack Exchange

This is a backtick. A backtick is not a quotation sign. It has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command (like chown in your examples), and the output of that execution is used by that command, just as if you'd type that output at that place in the command line.

So, what

sudo chown `id -u` /somedir

effectively runs (depending on your user ID) is:

sudo chown 1000 /somedir
  \    \     \     \
   \    \     \     `-- the second argument to "chown" (target directory)
    \    \     `-- your user ID, which is the output of "id -u" command
     \    `-- "chown" command (change ownership of file/directory)
      `-- the "run as root" command; everything after this is run with root privileges

Have a look at this question to learn why, in many situations, it is not a good idea to use backticks.

Btw, if you ever wanted to use a backtick literally, e.g. in a string, you can escape it by placing a backslash (\) before it.

Answer from rozcietrzewiacz on unix.stackexchange.com
🌐
Stack Exchange
unix.stackexchange.com › questions › 48392 › understanding-backtick
bash - Understanding backtick (`) - Unix & Linux Stack Exchange

Text between backticks is executed and replaced by the output of the command (minus the trailing newline characters, and beware that shell behaviors vary when there are NUL characters in the output). That is called command substitution because it is substituted with the output of the command. So if you want to print 5, you can't use backticks, you can use quotation marks, like echo "$b" or just drop any quotation and use echo $b.

As you can see, since $b contains 5, when using backticks bash is trying to run command 5 and since there is no such command, it fails with error message.

To understand how backticks works, try running this:

$ A=`cat /etc/passwd | head -n1`
$ echo "$A"

cat /etc/passwd |head -n1 should print first line of /etc/passwd file. But since we use backticks, it doesn't print this on console. Instead it is stored in A variable. You can echo $A to this. Note that more efficient way of printing first line is using command head -n1 /etc/passwd but I wanted to point out that expression inside of backticks does not have to be simple.

So if first line of /etc/passwd is root:x:0:0:root:/root:/bin/bash, first command will be dynamically substituted by bash to A="root:x:0:0:root:/root:/bin/bash".

Note that this syntax is of the Bourne shell. Quoting and escaping becomes quickly a nightmare with it especially when you start nesting them. Ksh introduced the $(...) alternative which is now standardized (POSIX) and supported by all shells (even the Bourne shell from Unix v9). So you should use $(...) instead nowadays unless you need to be portable to very old Bourne shells.

Also note that the output of `...` and $(...) are subject to word splitting and filename generation just like variable expansion (in zsh, word splitting only), so would generally need to be quoted in list contexts.

Answer from Krzysztof Adamski on unix.stackexchange.com
🌐
Greg's Wiki
mywiki.wooledge.org › BashFAQ › 082
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.
🌐
Super User
superuser.com › questions › 1203270 › what-is-the-purpose-of-cd-backtick
linux - What is the purpose of cd ` (backtick)? - Super User

What you've typed is a backtick - it is the start of an instruction to bash to evaluate what you type as a command. The > is displayed to indicate you are still entering the command on the next line.

If you close the backtick you'll find the whole command will run. E.g.

~$ cd `
> echo /var`
/var$
Answer from John C on superuser.com
🌐
Red Hat
redhat.com › en › blog › backtick-operator-vs-parens
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
linuxopsys.com › bash-backticks-vs-dollar-parentheses
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.
Find elsewhere
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › bash-when-to-use-backtick-and-when-to-use-$-490497
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.
🌐
Server Fault
serverfault.com › questions › 831424 › evaluation-of-backticks-in-bash
linux - Evaluation of backticks in bash - Server Fault

No, the backticks do not have a special meaning and you even could run the wget command without them in the if statement.
if always evaluates the exit code of the command followed.

A more comprehensive overview can be found here.


EDIT

Backticks initiate a command substitution which is executed in a subshell and returns an exit code. if just checks return codes, it does not make use of the output of the command.

To compare the output of a command you could use the [ , which must be closed with ], which basically is a test. The test could be anything

  • from man test
  • a string comparison

    [ "hello" == "test" ]
    
  • an integer test

    [ 2 -eq 3 ]  
    

If the test succeeds you get an exit code 0 ( true ), otherwise not 0 ( false ) which is again evaluated by if.

So you have something in mind like follows.

if [ "`wget google.com -T 2 -t 1 -o /dev/null`" == "" ]
then 
    echo "emptY"
fi

But this wouldn't make much sense, due to redirecting the output to /dev/null you will always get true from [ "wget google.com -T 2 -t 1 -o /dev/null" == "" ].
On the other hand it also would not be useful to check if the output of the wget command does contain output, leaving out the -o /dev/null, because even in an error situation you will get output, but different return codes.

Answer from Thomas on serverfault.com
🌐
Linux.org
linux.org › forums › general linux forums › command line
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...
🌐
Stack Overflow
stackoverflow.com › questions › 22709371 › backticks-vs-braces-in-bash
command substitution - Backticks vs braces in Bash - Stack Overflow

The `` is called Command Substitution and is almost equivalent to $() (parenthesis), while you are using ${} (curly braces).

So all of these expressions are equal and mean "interpret the command placed inside":

joulesFinal=`echo $joules2 \* $cpu | bc`
joulesFinal=$(echo $joules2 \* $cpu | bc)
#            v                          v
#      ( instead of {                   v
#                                 ) instead of }

While ${} expressions are used for variable substitution.

Note, though, that backticks are deprecated, while $() is POSIX compatible, so you should prefer the latter.


From man bash:

Command substitution allows the output of a command to replace the command name. There are two forms:

          $(command)
   or
          `command`
   

# later on in the man page:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Also, `` are more difficult to handle, you cannot nest them for example. See comments below and also Why is $(...) preferred over ... (backticks)?.

Answer from fedorqui on stackoverflow.com
🌐
Linux Hint
linuxhint.com › mastering-backticks-linux-bash-scripts
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
community.unix.com › shell programming and scripting
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 ?
🌐
Ask Ubuntu
askubuntu.com › questions › 487554 › using-backticks-or-dollar-in-shell-scripts
command line - Using backticks or dollar in shell scripts - Ask Ubuntu

Use dollar. Backticks are semi-deprecated, because they are more complicated to use (see the link), and there are no advantages to them unless you're doing code golf and absolutely need to save a single character. They probably won't be removed from popular shells anytime soon though, so you're safe using either for now.

Answer from l0b0 on askubuntu.com
🌐
Super User
superuser.com › questions › 1184659 › bash-backtick-vs-parenthesis
linux - Bash backtick vs parenthesis - Super User

What you are missing here is $.

There is 2 forms of command substitution on bash, backticks, as exemplified by you, and enclosing by $(command).

What you should use is:

#!/bin/bash

a=$(echo Hello!)

echo $a
Answer from giorni on superuser.com
🌐
Stack Overflow
stackoverflow.com › questions › 48552087 › using-a-command-that-needs-backticks-to-be-passed-as-part-of-an-argument-in-bash
linux - Using a command that needs backticks to be passed as part of an argument in bash - Stack Overflow

Inside double quotes, backticks are being interpreted by shell, resulting in table being treated as a command. You need to escape them:

hive -e "alter table ${database}.\`${table}\` SET TBLPROPERTIES('EXTERNAL'='FALSE')"

Alternatively, you can use a variable to hold the backticks:

bt="\`"
hive -e "alter table ${database}.$bt${table}$bt SET TBLPROPERTIES('EXTERNAL'='FALSE')"

The issue with your second command

hive -e "alter table ${database}.$(table) SET TBLPROPERTIES('EXTERNAL'='FALSE')"

is that the construct $(table) works the same way as table inside backticks - it is just a better way of doing command substitution in Bash. Hence you get the same error as earlier.


Related posts:

  • Difference between single and double quotes in Bash
  • What is the benefit of using $() instead of backticks in shell scripts?
Answer from codeforester on stackoverflow.com
🌐
Ask Ubuntu
askubuntu.com › questions › 20034 › differences-between-doublequotes-singlequotes-and-backticks-on-comm
command line - Differences between doublequotes " ", singlequotes ' ' and backticks ` ` on commandline? - Ask Ubuntu

For the sake of example, consider that variable foo contains uname (foo=uname).

  • echo "$foo" outputs uname, substituting variables in text.
    • For a literal $ character inside " quotes, use \$; for a literal ", use \".
  • echo '$foo' outputs $foo, the exact string.
    • Even ' can't be escaped as \' inside ' quotes. But you can use 'foo'\''bar'.
  • echo `$foo` outputs Linux, executing the content of the variable and echo printing it.
Answer from shellholic on askubuntu.com
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
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
thegeekdiary.com › backtick-symbol-in-linux-shell-scripting
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" $