Introduction to Unix

../_images/ksus5.jpg ../_images/UNIX_blocks5.jpeg

4.10. Command Substitution

Linux for Programmers and Users, Section 5.8

Sometimes we want to use the output of one command as part of another command. This may be because we can only effectively determine the desired information by running another command or because it is just easier to run a command than to type the output produced from the command. Command substitution allows us to do just that. The resulting command contains the embedded output of running the command substitution command.

There are two valid forms for expressing command substitution.

  1. Pair of grave accent characters – Surround the command with grave accents, also known as back-quotes (` `):

    $ ls -l `which less`
    -rwxr-xr-x 1 root root 139456 Mar 31  2010 /usr/bin/less
    
  2. Dollar sign and parenthesis ($( )):

    $ ls -l $(which less)
    -rwxr-xr-x 1 root root 139456 Mar 31  2010 /usr/bin/less
    

Note

The advantage of the grave accent is that it is more compact and might be more familiar to some casual Unix users. The later form is perhaps more explicit and will not likely be confused with regular single quote characters.