Variables can be declared as integers with the declare -i command. If you attempt to assign any string value, bash assigns 0 to the variable. Arithmetic can be performed on variables that have been declared as integers. (If the variable has not been declared as an integer, the built-in let command allows arithmetic operations. See "The let Command" on page 394.) If you attempt to assign a floating point number, bash reports a syntax error. Numbers can also be represented in different bases such as binary, octal, and hex.
1 $ declare –i num 2 $ num=hello $ echo $num 0 3 $ num=5 + 5 bash: +: command not found 4 $ num=5+5 $ echo $num 10 5 $ num=4*6 $ echo $num 24 6 $ num="4 * 6" $ echo $num 24 7 $ num=6.5 bash: num: 6.5: sytax error in expression (remainder of expression is ".5") |
Explanation
Trying to assign the string hello to the integer variable num causes the string to be stored as zero.
The white space must be quoted or removed unless the let and is used.
The white space is removed and arithmetic is performed.
Multiplication is performed and the result assigned to num.
The white space is quoted so that the multiplication can be performed and to keep the shell from expanding the wildcard (*).
Because the variable is set to integer, adding a fractional part causes a bash syntax error.
The declare command with only the -i argument will list all preset integers and their values, as shown in the following display.
$ declare –i declare -ir EUID="15" # effective user id declare -ir PPID="235" # parent process id declare -ir UID="15" # user id
Numbers can be represented in decimal (base 10 and the default), octal (base 8), hexadecimal (base 16), ranging from base 2 to 36.
Format
variable=base#number-in-that-base
n=2#101 Base is 2; number 101 is in base 2 |
(The Command Line) 1 $ declare -i x=017 $ echo $x 15 2 $ x=2#101 $ echo $x 5 3 $ x=8#17 $ echo $x 15 4 $ x=16#b $ echo $x 11 |
Explanation
The declare function is used to assign an integer variable x the octal value 017. Octal numbers must start with a leading zero. 15, the decimal value of 017, is printed.
The variable, x, is assigned the value of 17 (octal).x. The value of x is printed as decimal, 15.
The variable, x, is assigned the value of b (hexadecimal). The value of x is decimal 11.
The let command is a bash shell built-in command that is used to perform integer arithmetic and numeric expression testing. To see what let operators your version of bash supports, type at the prompt:
help let
A list of the operators is also found in Table 9.4 on page 407.
1 $ i=5 or let i=5 2 $ let i=i+1 $ echo $i 6 3 $ let "i = i + 2" $ echo $i 8 4 $ let "i+=1" $ echo $i 9 5 $ i=3 6 $ (( i+=4)) $ echo $i 7 7 $ (( i=i-2 )) $ echo $i 5 |
Explanation
The variable i is assigned the value 5.
The let command will add 1 to the value of i. The $ (dollar sign) is not required for variable substitution when performing arithmetic.
The quotes are needed if the arguments contain white space.
The shortcut operator, +=, is used to add 1 to the value of i.
The variable i is assigned the value 5.
2 is subtracted from i. We could have also written: i–=2
Bash supports only integer arithmetic, but the bc, awk, and nawk utilities are useful if you need to perform more complex calculations.
(The Command Line) 1 $ n=`echo "scale=3; 13 / 2" | bc` $ echo $n 6.500 2 product=`gawk -v x=2.45 -v y=3.123'BEGIN{printf "%.2f ",x*y}'' $ echo $product 7.65 |
Explanation
The output of the echo command is piped to the bc program. The scale is set to 3, which is the number of significant digits to the right of the decimal point that will be printed. The calculation is to divide 13 by 2. The entire pipeline is enclosed in back quotes. Command substitution will be performed and the output assigned to the variable n.
The gawk program gets its values from the argument list passed in at the command line, x=2.45 y=3.123. After the numbers are multiplied, the printf function formats and prints the result with a precision of two places to the right of the decimal point. The output is assigned to the variable product.
3.145.78.136