3.3 Command Substitution
3.3 Command Substitution
evaluated. If any of the variables user, acct or bin are not set then the shell will abandon execution of the procedure.
is equivalent to
d=/usr/fred/bin
The entire string between grave accents (`...`) is taken as the command to be executed and is replaced with the output from the command. The command is written using the usual quoting conventions except that a ` must be escaped using a \. For example,
ls `echo "$1"`
is equivalent to
ls $1
Command substitution occurs in all contexts where parameter substitution occurs (including here documents) and the treatment of the resulting text is the same in both cases. This mechanism allows string processing commands to be used within shell procedures. An example of such a command is basename which removes a specified suffix from a string. For example,
basename main.c .c
will print the string main. Its use is illustrated by the following fragment from a cc command.
case $A in ... *.c) ... esac
B=`basename $A .c`
The shell is a macro processor that provides parameter substitution, command substitution and file name generation for the arguments to commands. This section discusses the order in which these evaluations occur and the effects of the various quoting mechanisms.
Commands are parsed initially according to the grammar given in appendix A. Before a command is executed the following substitutions occur.
parameter substitution, e.g. $user command substitution, e.g. `pwd` Only one evaluation occurs so that if, for example, the value of the variable X is the string $y then
echo $X