3.6. Operations and Related Topics

3.6.1. Operations

=

All-purpose assignment operator, which works for both arithmetic and string assignments.

   1 var=27
   2 category=minerals

May also be used in a string comparison test.

   1 if [ $string1 = $string2 ]
   2 then
   3    command
   4 fi

The following are normally used in combination with expr or let.

arithmetic operators

+

plus

-

minus

*

multiplication

/

division

%

modulo, or mod (returns the remainder of an integer division)

+=

"plus-equal" (increment variable by a constant)

`expr $var+=5` results in var being incremented by 5.

-=

"minus-equal" (decrement variable by a constant)

*=

"times-equal" (multiply variable by a constant)

`expr $var*=4` results in var being multiplied by 4.

/=

"slash-equal" (divide variable by a constant)

%=

"mod-equal" (remainder of dividing variable by a constant)

The bitwise logical operators seldom make an appearance in shell scripts. Their chief use seems to be manipulating and testing values read from ports or sockets. "Bit flipping" is more relevant to compiled languages, such as C and C++, which run fast enough to permit its use on the fly.

<<

bitwise left shift (multiplies by 2 for each shift position)

<<=

"left-shift-equal"

let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)

>>

bitwise right shift (divides by 2 for each shift position)

>>=

"right-shift-equal" (inverse of <<=)

&

bitwise and

&=

"bitwise and-equal"

|

bitwise OR

|=

"bitwise OR-equal"

~

bitwise negate

!

bitwise NOT

^

bitwise XOR

^=

"bitwise XOR-equal"

relational tests

<

less than

>

greater than

<=

less than or equal to

>=

greater than or equal to

==

equal to (test)

!=

not equal to

&&

and (logical)

   1 if [ $condition1 ] && [ $condition2 ]
   2 # if both condition1 and condition2 hold true...

Note

&& may also, depending on context, be used to in an and list to concatenate commands (see Section 3.21).

||

or (logical)

   1 if [ $condition1 ] || [ $condition2 ]
   2 # if both condition1 or condition2 hold true...


Example 3-15. Compound Condition Tests Using && and ||

   1 #!/bin/bash
   2 
   3 a=24
   4 b=47
   5 
   6 if [ $a -eq 24 ] && [ $b -eq 47 ]
   7 then
   8   echo "Test #1 succeeds."
   9 else
  10   echo "Test #1 fails."
  11 fi
  12 
  13 # ERROR:
  14 # if [ $a -eq 24 && $b -eq 47 ]
  15 
  16 
  17 if [ $a -eq 98 ] || [ $b -eq 47 ]
  18 then
  19   echo "Test #2 succeeds."
  20 else
  21   echo "Test #2 fails."
  22 fi
  23 
  24 
  25 # The -a and -o options provide
  26 # an alternative compound condition test.
  27 # Thanks to Patrick Callahan for pointing this out.
  28 
  29 
  30 if [ $a -eq 24 -a $b -eq 47 ]
  31 then
  32   echo "Test #3 succeeds."
  33 else
  34   echo "Test #3 fails."
  35 fi
  36 
  37 
  38 if [ $a -eq 98 -o $b -eq 47 ]
  39 then
  40   echo "Test #4 succeeds."
  41 else
  42   echo "Test #4 fails."
  43 fi
  44 
  45 
  46 a=rhino
  47 b=crocodile
  48 if [ $a = rhino ] && [ $b = crocodile ]
  49 then
  50   echo "Test #5 succeeds."
  51 else
  52   echo "Test #5 fails."
  53 fi
  54 
  55 exit 0

3.6.2. Numerical Constants

A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation. A number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16). A number with an embedded # is evaluated as BASE#NUMBER (this option is of limited usefulness because of range restrictions).


Example 3-16. Representation of numerical constants:

   1 #!/bin/bash
   2 
   3 # Representation of numbers.
   4 
   5 # Decimal
   6 let "d = 32"
   7 echo "d = $d"
   8 # Nothing out of the ordinary here.
   9 
  10 
  11 # Octal: numbers preceded by '0'
  12 let "o = 071"
  13 echo "o = $o"
  14 # Expresses result in decimal.
  15 
  16 # Hexadecimal: numbers preceded by '0x' or '0X'
  17 let "h = 0x7a"
  18 echo "h = $h"
  19 
  20 # Other bases: BASE#NUMBER
  21 # BASE between 2 and 64.
  22 let "b = 32#77"
  23 echo "b = $b"
  24 # This notation only works for a very limited range of numbers.
  25 let "c = 2#47"  # Error: out of range.
  26 echo "c = $c"
  27 
  28 
  29 exit 0