| < Day Day Up > |
13.13. Arithmetic Expansion
The shell
There are two formats for evaluating arithmetic expressions: FORMAT $[ expression ] $(( expression )) Example 13.77.echo $[ 5 + 4 - 2 ] 7 echo $[ 5 + 3 * 2] 11 echo $[(5 + 3) * 2] 16 echo $(( 5 + 4 )) 9 echo $(( 5 / 0 )) bash: 5/0: division by 0 ( error token is "0") |
| < Day Day Up > |
| < Day Day Up > |
13.14. Order of Expansion
When you are performing the expansion of variables, commands, arithmetic expressions, and pathnames, the shell is programmed to follow a specific order when scanning the command line. Assuming that the
|
| < Day Day Up > |
| < Day Day Up > |
13.15. Arrays
Versions of
bash
2.x provide for creation of one-dimensional arrays. Arrays allow you to collect a list of words into one variable
FORMAT declare -a variable_name variable = ( item1 item2 item3 ... ) Example 13.78.declare -a nums=(45 33 100 65) declare -ar names (array is readonly) names=( Tom Dick Harry) states=( ME [3]=CA CT ) x[0]=55 n[4]=100 When assigning values to an array, they are automatically started at index 0 and incremented by 1 for each additional element added. You do not have to provide indices in an assignment, and if you do, they do not have to be in order. To unset an array, use the unset command followed by the array name, and to unset one element of the array, use unset and the arrayname[subscript] syntax.
The
declare
,
local
, and
read-only
Example 13.79.1 $ declare -a friends 2 $ friends=(Sheryl Peter Louise) 3 $ echo ${friends[0]} Sheryl 4 $ echo ${friends[1]} Peter 5 $ echo ${friends[2]} Louise 6 $ echo " All the friends are ${friends[*]}" All the friends are Sheryl Peter Louise 7 $ echo "The number of elements in the array is ${#friends[*]}" The number of elements in the array is 3 8 $ unset friends or unset ${friends[*]} EXPLANATION
Example 13.80.1 $ x[3]=100 $ echo ${x[*]} 100 2 $ echo ${x[0]} 3 $ echo ${x[3]} 100 4 $ states=(ME [3]=CA [2]=CT) $ echo ${states[*]} ME CA CT 5 $ echo ${states[0]} ME 6 $ echo ${states[1]} 7 $ echo ${states[2]} CT 8 $ echo ${states[3]} CA EXPLANATION
|
| < Day Day Up > |