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


Operator Precedence (How Operators Nest)

Operator precedence determines how operators are grouped, when different operators appear close by in one expression. For example, `*' has higher precedence than `+'; thus, a + b * c means to multiply b and c, and then add a to the product (i.e., a + (b * c)).

You can overrule the precedence of the operators by using parentheses. You can think of the precedence rules as saying where the parentheses are assumed if you do not write parentheses yourself. In fact, it is wise to always use parentheses whenever you have an unusual combination of operators, because other people who read the program may not remember what the precedence is in this case. You might forget, too; then you could make a mistake. Explicit parentheses will help prevent any such mistake.

When operators of equal precedence are used together, the leftmost operator groups first, except for the assignment, conditional and exponentiation operators, which group in the opposite order. Thus, a - b + c groups as (a - b) + c; a = b = c groups as a = (b = c).

The precedence of prefix unary operators does not matter as long as only unary operators are involved, because there is only one way to parse them--innermost first. Thus, $++i means $(++i) and ++$x means ++($x). However, when another operator follows the operand, then the precedence of the unary operators can matter. Thus, $x^2 means ($x)^2, but -x^2 means -(x^2), because `-' has lower precedence than `^' while `$' has higher precedence.

Here is a table of the operators of awk, in order of increasing precedence:

assignment
`=', `+=', `-=', `*=', `/=', `%=', `^=', `**='. These operators group right-to-left. (The `**=' operator is not specified by POSIX.)
conditional
`?:'. This operator groups right-to-left.
logical "or".
`||'.
logical "and".
`&&'.
array membership
`in'.
matching
`~', `!~'.
relational, and redirection
The relational operators and the redirections have the same precedence level. Characters such as `>' serve both as relationals and as redirections; the context distinguishes between the two meanings. The relational operators are `<', `<=', `==', `!=', `>=' and `>'. The I/O redirection operators are `<', `>', `>>' and `|'. Note that I/O redirection operators in print and printf statements belong to the statement level, not to expressions. The redirection does not produce an expression which could be the operand of another operator. As a result, it does not make sense to use a redirection operator near another operator of lower precedence, without parentheses. Such combinations, for example `print foo > a ? b : c', result in syntax errors.
concatenation
No special token is used to indicate concatenation. The operands are simply written side by side.
add, subtract
`+', `-'.
multiply, divide, mod
`*', `/', `%'.
unary plus, minus, "not"
`+', `-', `!'.
exponentiation
`^', `**'. These operators group right-to-left. (The `**' operator is not specified by POSIX.)
increment, decrement
`++', `--'.
field
`$'.


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