Command substitution

bash offers a handy ability to do command substitution with the $(command) or `command` syntax. Wherever $(command) is found, its output is substituted prior to interpretation by the shell. For example, to set a variable to the number of lines in your .bashrc file, you could use wc -l:

$ RCSIZE=$(wc -l ~/.bashrc)

Another form of command substitution encloses command in backquotes or backticks (`):

$ RCSIZE=`wc -l ~/.bashrc`

The result is the same, except that the backquote syntax allows the backslash character to escape the dollar symbol ($), the backquote (`), and another backslash (\). The $(command) syntax avoids this nuance by treating all characters between the parentheses literally.

Get LPI Linux Certification in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.