gawk: Precedence
1
1 6.5 Operator Precedence (How Operators Nest)
1 ============================================
1
1 "Operator precedence" determines how operators are grouped when
1 different operators appear close by in one expression. For example, '*'
1 has higher precedence than '+'; thus, 'a + b * c' means to multiply 'b'
1 and 'c', and then add 'a' to the product (i.e., 'a + (b * c)').
1
1 The normal precedence of the operators can be overruled by using
1 parentheses. Think of the precedence rules as saying where the
1 parentheses are assumed to be. In fact, it is wise to always use
1 parentheses whenever there is an unusual combination of operators,
1 because other people who read the program may not remember what the
1 precedence is in this case. Even experienced programmers occasionally
1 forget the exact rules, which leads to mistakes. Explicit parentheses
1 help prevent any such mistakes.
1
1 When operators of equal precedence are used together, the leftmost
1 operator groups first, except for the assignment, conditional, and
1 exponentiation operators, which group in the opposite order. Thus, 'a -
1 b + c' groups as '(a - b) + c' and 'a = b = c' groups as 'a = (b = c)'.
1
1 Normally the precedence of prefix unary operators does not matter,
1 because there is only one way to interpret them: innermost first. Thus,
1 '$++i' means '$(++i)' and '++$x' means '++($x)'. However, when another
1 operator follows the operand, then the precedence of the unary operators
1 can matter. '$x^2' means '($x)^2', but '-x^2' means '-(x^2)', because
1 '-' has lower precedence than '^', whereas '$' has higher precedence.
1 Also, operators cannot be combined in a way that violates the precedence
1 rules; for example, '$$0++--' is not a valid expression because the
1 first '$' has higher precedence than the '++'; to avoid the problem the
1 expression can be rewritten as '$($0++)--'.
1
1 This list presents 'awk''s operators, in order of highest to lowest
1 precedence:
1
1 '('...')'
1 Grouping.
1
1 '$'
1 Field reference.
1
1 '++ --'
1 Increment, decrement.
1
1 '^ **'
1 Exponentiation. These operators group right to left.
1
1 '+ - !'
1 Unary plus, minus, logical "not."
1
1 '* / %'
1 Multiplication, division, remainder.
1
1 '+ -'
1 Addition, subtraction.
1
1 String concatenation
1 There is no special symbol for concatenation. The operands are
1 simply written side by side (⇒Concatenation).
1
1 '< <= == != > >= >> | |&'
1 Relational and redirection. The relational operators and the
1 redirections have the same precedence level. Characters such as
1 '>' serve both as relationals and as redirections; the context
1 distinguishes between the two meanings.
1
1 Note that the I/O redirection operators in 'print' and 'printf'
1 statements belong to the statement level, not to expressions. The
1 redirection does not produce an expression that could be the
1 operand of another operator. As a result, it does not make sense
1 to use a redirection operator near another operator of lower
1 precedence without parentheses. Such combinations (e.g., 'print
1 foo > a ? b : c') result in syntax errors. The correct way to
1 write this statement is 'print foo > (a ? b : c)'.
1
1 '~ !~'
1 Matching, nonmatching.
1
1 'in'
1 Array membership.
1
1 '&&'
1 Logical "and."
1
1 '||'
1 Logical "or."
1
1 '?:'
1 Conditional. This operator groups right to left.
1
1 '= += -= *= /= %= ^= **='
1 Assignment. These operators group right to left.
1
1 NOTE: The '|&', '**', and '**=' operators are not specified by
1 POSIX. For maximum portability, do not use them.
1