What does ` (backquote/backtick) mean in commands?

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
Asked By: gws


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

for example:

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

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

Answered By: Hanan N.

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.

Answered By: Julian

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.

Answered By: rozcietrzewiacz

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.

Answered By: Kannan Mohan

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:


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 :)
Answered By: Dave K

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:


We could then print the result: ‘The date is %sn’ “$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.
Answered By: user9303970
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.