Go to the first, previous, next, last section, table of contents.

Assignment Expressions

An assignment is an expression that stores a new 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 store string values also. For example, this would store 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 right-hand operand is stored unchanged.

Most operators (addition, concatenation, and so on) have no effect except to compute a value. If you ignore the value, you might as well not use the operator. An assignment operator is different; it does produce a value, but even if you ignore the value, the assignment still makes itself felt through the alteration of the variable. We call this a side effect.

The left-hand operand of an assignment need not be a variable (see section Variables); it can also be a field (see section Changing the Contents of a Field) or an array element (see section Arrays in awk). These are all called lvalues, which means they can appear on the left-hand side of an assignment operator. The right-hand operand may be any expression; it produces the new value which 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. The type of a variable 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 this code, the value of foo is five:

foo = "a string"
foo = foo + 5

(Note that using a variable as a number and then later as a string can be confusing and is poor programming style. The above examples illustrate how awk works, not how you should write your own programs!)

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

x = y = z = 0

stores the value zero in all three variables. It does this because the value of `z = 0', which is zero, is stored into y, and then the value of `y = z = 0', which is zero, is stored into x.

You can use an assignment 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; except in a one-shot program, you should not use such nesting of assignments.

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 right-hand 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 one makes the meaning of your program clearer.

There are situations where using `+=' (or any assignment operator) is not the same as simply repeating the left-hand operand in the right-hand expression. For example:

# Thanks to Pat Rankin for this example
    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 guaranteed to be different, because rand will return different values each time it is called. (Arrays and the rand function haven't been covered yet. See section Arrays in awk, and see section Numeric Built-in Functions, for more information). This example illustrates an important fact about the assignment operators: the left-hand expression is only evaluated once.

It is also up to the implementation as to which expression is evaluated first, the left-hand one or the right-hand one. Consider this example:

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

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

Here is a table of the arithmetic assignment operators. In each case, the right-hand operand is an expression whose value is converted to a number.

lvalue += increment
Adds increment to the value of lvalue to make the new 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. (Only the `^=' operator is specified by POSIX.)

For maximum portability, do not use the `**=' operator.

Go to the first, previous, next, last section, table of contents.