Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

4 Answers 4

up vote 30 down vote accepted

This is a backtick. 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.

share|improve this answer
6  
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
3  
@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
    
@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

This symbol mean 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 resutl will be that the three directory will be copied to my /home directory.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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