Next: , Previous: Concatenation, Up: All Operators


6.2.3 Assignment Expressions

An assignment is an expression that stores a (usually different) value into a variable. For example, let's assign the value one to the variable z:

     z = 1

After this expression is executed, the variable z has the value one. Whatever old value z had before the assignment is forgotten.

Assignments can also store string values. For example, the following stores the value "this food is good" in the variable message:

     thing = "food"
     predicate = "good"
     message = "this " thing " is " predicate

This also illustrates string concatenation. The ‘=’ sign is called an assignment operator. It is the simplest assignment operator because the value of the righthand operand is stored unchanged. Most operators (addition, concatenation, and so on) have no effect except to compute a value. If the value isn't used, there's no reason to use the operator. An assignment operator is different; it does produce a value, but even if you ignore it, the assignment still makes itself felt through the alteration of the variable. We call this a side effect.

The lefthand operand of an assignment need not be a variable (see Variables); it can also be a field (see Changing Fields) or an array element (see Arrays). These are all called lvalues, which means they can appear on the lefthand side of an assignment operator. The righthand operand may be any expression; it produces the new value that the assignment stores in the specified variable, field, or array element. (Such values are called rvalues.)

It is important to note that variables do not have permanent types. A variable's type is simply the type of whatever value it happens to hold at the moment. In the following program fragment, the variable foo has a numeric value at first, and a string value later on:

     foo = 1
     print foo
     foo = "bar"
     print foo

When the second assignment gives foo a string value, the fact that it previously had a numeric value is forgotten.

String values that do not begin with a digit have a numeric value of zero. After executing the following code, the value of foo is five:

     foo = "a string"
     foo = foo + 5
NOTE: Using a variable as a number and then later as a string can be confusing and is poor programming style. The previous two examples illustrate how awk works, not how you should write your programs!

An assignment is an expression, so it has a value—the same value that is assigned. Thus, ‘z = 1’ is an expression with the value one. One consequence of this is that you can write multiple assignments together, such as:

     x = y = z = 5

This example stores the value five in all three variables (x, y, and z). It does so because the value of ‘z = 5’, which is five, is stored into y and then the value of ‘y = z = 5’, which is five, is stored into x.

Assignments may be used anywhere an expression is called for. For example, it is valid to write ‘x != (y = 1)’ to set y to one, and then test whether x equals one. But this style tends to make programs hard to read; such nesting of assignments should be avoided, except perhaps in a one-shot program.

Aside from ‘=’, there are several other assignment operators that do arithmetic with the old value of the variable. For example, the operator ‘+=’ computes a new value by adding the righthand value to the old value of the variable. Thus, the following assignment adds five to the value of foo:

     foo += 5

This is equivalent to the following:

     foo = foo + 5

Use whichever makes the meaning of your program clearer.

There are situations where using ‘+=’ (or any assignment operator) is not the same as simply repeating the lefthand operand in the righthand expression. For example:

     # Thanks to Pat Rankin for this example
     BEGIN  {
         foo[rand()] += 5
         for (x in foo)
            print x, foo[x]
     
         bar[rand()] = bar[rand()] + 5
         for (x in bar)
            print x, bar[x]
     }

The indices of bar are practically guaranteed to be different, because rand() returns different values each time it is called. (Arrays and the rand() function haven't been covered yet. See Arrays, and see Numeric Functions, for more information). This example illustrates an important fact about assignment operators: the lefthand expression is only evaluated once. It is up to the implementation as to which expression is evaluated first, the lefthand or the righthand. Consider this example:

     i = 1
     a[i += 2] = i + 1

The value of a[3] could be either two or four.

table-assign-ops lists the arithmetic assignment operators. In each case, the righthand operand is an expression whose value is converted to a number.

Operator Effect
lvalue += increment Adds increment to the value of lvalue.
lvalue -= decrement Subtracts decrement from the value of lvalue.
lvalue *= coefficient Multiplies the value of lvalue by coefficient.
lvalue /= divisor Divides the value of lvalue by divisor.
lvalue %= modulus Sets lvalue to its remainder by modulus.
lvalue ^= power
lvalue **= power Raises lvalue to the power power. (c.e.)

Table 6.2: Arithmetic Assignment Operators

NOTE: Only the ‘^=’ operator is specified by POSIX. For maximum portability, do not use the ‘**=’ operator.

Syntactic Ambiguities Between ‘/=’ and Regular Expressions

There is a syntactic ambiguity between the /= assignment operator and regexp constants whose first character is an ‘=’. (d.c.) This is most notable in some commercial awk versions. For example:

     $ awk /==/ /dev/null
     error--> awk: syntax error at source line 1
     error-->  context is
     error-->         >>> /= <<<
     error--> awk: bailing out at source line 1

A workaround is:

     awk '/[=]=/' /dev/null

gawk does not have this problem, nor do the other freely available versions described in Other Versions.