157

I came across the following command:

sudo chown `id -u` /somedir

and I wonder: what is the meaning of the ` symbol. I noticed for instance that while the command above works well, the one below does not:

sudo chown 'id -u' /somedir
197

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.

  • 36
    This explains backticks pretty well, but using $(your expression)is a better way to do the same thing as $() allows you to nest expressions. for instance: cd $(dirname $(type -P touch)) will cd you into the directory containing the touch command – Khaja Minhajuddin Dec 27 '11 at 1:10
  • 11
    @KhajaMinhajuddin You're definitely right about nesting - the above mentioned question covers it in detail. But even though I think it is a good practise to use $() in most situations, it does not make backticks a worse thing. For practical purposes, one has to admit that they are much faster to type on the command line (2 keystrokes compared to at least 5, including Shift). – rozcietrzewiacz Oct 23 '12 at 13:14
  • 2
    @rozcietrzewiacz Your latter remark is probably true for most keyboards but $( ) is definitely easier to type than ` ` at least on a French keyboard. – jlliagre May 19 '13 at 20:38
  • 1
    @KhajaMinhajuddin You can nest backticks, but you need to escape the 2nd level of backticks, & 2nd level nesting you need 3 backticks, 3rd level nesting 5 backticks, 4th level 7, &c. – Justin C. B. Jul 18 '18 at 14:49
65

I would like to add few more points here.

The backtick `…` is actually called command substitution. The purpose of command substitution is to evaluate the command which is placed inside the backtick and provide its result as an argument to the actual command.

The command substitution can be done in two ways one is using $(…) and the other is `…`. Both work same, but the $(…) form is the modern way and has more clarity and readability.

And so

sudo chown $(id -u) /somedir

can be preferred over the other.

And one more thing you need to note here is the command substitution relationship with the bash quoting rules as mentioned in the bash document.

If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

  • 8
    Also, $(...) nests better. – Kusalananda Jun 28 '16 at 11:56
  • $() and "``" do not work the same way – Kun Jun 21 '18 at 22:31
10

One note of clarification rarely covered:

Backticks (sometimes also called Graves because it doubles as a common accent in French and other languages) substitute the Standard Output only, but not the Standard Error.

So to continue the previous example:

file `which hostname`

will work as expected, but in:

file `which hostnameX`

which will return an error, and that output goes to standard error, rather than substituting onto the command line next to file; there will be no standard output at all, which you can confirm by running:

which hostnameX

which hostnameX 2>/dev/null

In this case,

file `which hostnameX` 

will produce two error messages (the first one, due to which hostnameXand the second one just after the former, due to file itself, that finds that the file name is missing and so, the whole command

will essentially reduce to just:

file

which, of course, is bad usage, and will return a Usage error.

(if you want to check it by yourself you can try:

file `which hostnameX 2>/dev/null`    # just the file-command bad-usage error msg is printed

file `which hostnameX 2>/dev/null` 2>/dev/null  # now nothing is printed on the screen :)
4

The backtick ` runs the contents of the enclosed string, so something like this

file `which hostname`

will find out the path to the hostname command, and then tell you how it was built.

The command that you put in your question runs id -u to get the effective user id, and then changes the ownership of /somedir to that user.

2

This symbol means that whatever inside it is interpreted as the result of that command.

for example:

$ls /home
one two
$pwd
/usr/three
$cp `pwd` /home
$ls /home
one two three

The above results in the three directory being copied to my /home directory.

1

The backticks resemble command substitution. This backticks syntax is archaic, and know the dollar sign with two parenthesis is common: $().

What is command substitution?

Command substitution is a single operation with dedicated syntax to both execute a command and to have its output stored into a variable for later use.

An example with date:

thedate=$(date)

We could then print the result: 'The date is %s\n' "$thedate"`.

  1. The command substitution syntax is $().
  2. The command itself is date.
  3. Combining both we get $(date) which its output is the substitution.
  4. We display the output value hold by the variable with printf, per the command above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.