Previous | Table of Contents | Next |
Two additional forms of substitution provided by the shell are
Command substitution enables you to capture the output of a command and substitute it in another command, whereas arithmetic substitution enables you to perform simple integer mathematics using the shell.
Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands. Command substitution is performed when a command is given as
`command`
Here command, can be a simple command, a pipeline, or a list.
Caution:
Make sure that you are using the backquote, not the single quote character, when performing command substitution. Command substitution is performed by the shell only when the backquote, or backtick, character, ', is given. Using the single quote instead of the back quote is a common error in shell scripts leading to many hard to find bugs.
Command substitution is generally used to assign the output of a command to a variable. Each of the following examples demonstrate command substitution:
DATE=`date` USERS=`who | wc l` UP=`date ; uptime`
In the first example, the output of the date command becomes the value for the variable DATE. In the second example, the output of the pipeline becomes the value of the variable USERS. In the last example, the output of the list becomes the value of the variable UP.
You can also use command substitution to provide arguments for other commands. For example,
grep `id un` /etc/passwd
looks through the file /etc/passwd for the output of the command:
id un
In my case, the command substitution results in the string ranga, and thus grep returns the entry in the passwd file for my user name.
In ksh and bash, the shell enables integer arithmetic to be performed. This avoids having to run an extra program such as expr or bc to do math in a shell script. This feature is not available in sh.
Arithmetic substitution is performed when the following form of command is given:
$((expression))
Expressions are evaluated according to standard mathematical conventions. Table 8.3 provides the available operators. The operators are listed in order of precedence.
Operator | Description |
---|---|
/ | The division operator. Divides two numbers and returns the result. |
* | The multiplication operator. Multiples two numbers and returns the result. |
- | The subtraction operator. Subtracts two numbers and returns the result. |
+ | The addition operator. Adds two numbers and returns the result. |
() | The parentheses clarify which expressions should be evaluated before others. |
You use the following command as an illustration of the operators and their precedence:
foo=$(( ((5 + 3*2) - 4) / 2 ))
After this command executes the value of foo to 3. Because this is integer arithmetic, the value is not 3.5, and because of operator precedence the value is not 6.
In this chapter, you have looked at the four main forms of substitution available in the shell:
As you write scripts and use the shell to solve problems, you find that these types of substitution help you extensively.
: ${MYPATH:=/usr/bin:/usr/sbin:/usr/ucb}
: ${MYPATH:-/usr/bin:/usr/sbin:/usr/ucb}
$(( 3 * 2 + ( 4 3 / 4) ))
Previous | Table of Contents | Next |