OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Assignment

Although its uses may not be immediately apparent, arithmetic expressions may perform assignment. We have performed assignment many times, though in a different context. Each time we give a variable a value, we are performing assignment. We can also do it within arithmetic expressions:



[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo $foo


[me@linuxbox ~]$ if (( foo = 5 )); then echo "It is true."; fi

It is true.

[me@linuxbox ~]$ echo $foo

5

[me@linuxbox ~]$ foo=

[me@linuxbox ~]$ echo $foo


[me@linuxbox ~]$ if (( foo = 5 )); then echo "It is true."; fi

It is true.

[me@linuxbox ~]$ echo $foo

5


In the example above, we first assign an empty value to the variable foo and verify that it is indeed empty. Next, we perform an if with the compound command (( foo = 5

)). This process does two interesting things: 1) it assigns the value of 5 to the variable

foo, and 2) it evaluates to true because foo was assigned a nonzero value.


image

Note: It is important to remember the exact meaning of the = in the expression above. A single = performs assignment. foo = 5 says “make foo equal to 5,” while == evaluates equivalence. foo == 5 says “does foo equal 5?” This is a common feature in many programming languages. In the shell, this can be a little confusing because the test command accepts a single = for string equivalence. Yet another reason to use the more modern [[ ]] and (( )) compound com- mands in place of test.


image

In addition to the =, the shell also provides notations that perform some very useful as- signments:


Table 34-4: Assignment Operators


Notation Description

Notation Description


image

parameter = value Simple assignment. Assigns value to parameter.


image

parameter += value Addition. Equivalent to parameter = parameter +

value.


image

parameter -= value Subtraction. Equivalent to parameter = parameter

value.


image

parameter *= value Multiplication. Equivalent to parameter = parameter

* value.


image

parameter /= value Integer division. Equivalent to parameter =

parameter / value.


image

parameter %= value Modulo. Equivalent to parameter = parameter %

value.


image

parameter++ Variable post-increment. Equivalent to parameter =

parameter + 1 (however, see discussion below).


image

parameter−− Variable post-decrement. Equivalent to parameter =

parameter − 1.


image

++parameter Variable pre-increment. Equivalent to parameter =

parameter + 1.


image

--parameter Variable pre-decrement. Equivalent to parameter =

parameter − 1.


image


These assignment operators provide a convenient shorthand for many common arithmetic tasks. Of special interest are the increment (++) and decrement (−−) operators, which in- crease or decrease the value of their parameters by one. This style of notation is taken from the C programming language and has been incorporated into a number of other pro- gramming languages, including bash.

The operators may appear either at the front of a parameter or at the end. While they both either increment or decrement the parameter by one, the two placements have a subtle difference. If placed at the front of the parameter, the parameter is incremented (or decre- mented) before the parameter is returned. If placed after, the operation is performed after the parameter is returned. This is rather strange, but it is the intended behavior. Here is a demonstration:



[me@linuxbox ~]$ foo=1 [me@linuxbox ~]$ echo $((foo++)) 1

[me@linuxbox ~]$ echo $foo

[me@linuxbox ~]$ foo=1 [me@linuxbox ~]$ echo $((foo++)) 1

[me@linuxbox ~]$ echo $foo


2

2


If we assign the value of one to the variable foo and then increment it with the ++ opera- tor placed after the parameter name, foo is returned with the value of one. However, if we look at the value of the variable a second time, we see the incremented value. If we place the ++ operator in front of the parameter, we get the more expected behavior:


[me@linuxbox ~]$ foo=1 [me@linuxbox ~]$ echo $((++foo)) 2

[me@linuxbox ~]$ echo $foo

2

[me@linuxbox ~]$ foo=1 [me@linuxbox ~]$ echo $((++foo)) 2

[me@linuxbox ~]$ echo $foo

2


For most shell applications, prefixing the operator will be the most useful.

The ++ and -- operators are often used in conjunction with loops. We will make some im- provements to our modulo script to tighten it up a bit:



#!/bin/bash

# modulo2: demonstrate the modulo operator for ((i = 0; i <= 20; ++i )); do

if (((i % 5) == 0 )); then printf "<%d> " $i

else

printf "%d " $i

fi done

printf "\n"

#!/bin/bash

# modulo2: demonstrate the modulo operator for ((i = 0; i <= 20; ++i )); do

if (((i % 5) == 0 )); then printf "<%d> " $i

else

printf "%d " $i

fi done

printf "\n"


Top OS Cloud Computing at OnWorks: