Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท AWK Basics

AWK Variables, Types and Operators

Learn how AWK variables work: automatic initialization, number and string conversion, arithmetic, concatenation, assignment and increment operators, precedence and passing values with -v.

AWK variables are the easiest of any language: you never declare them, you never give them a type, and you rarely worry about conversions. That convenience hides a few rules that are worth knowing, because they decide whether 10 > 9 is true and what "3x" + 1 means. In this lesson you will learn how variables are created, how AWK converts between numbers and strings, the operators and their precedence, and how to pass values from the shell.

Variables spring into existence

A variable is created the first time you use it. Its initial value is both the number 0 and the empty string "", depending on where it is used. This is why counters need no setup:

Bash Uninitialized variables are zero or empty

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

count++ works because an uninitialized variable counts as 0 in a numeric context. x printed as text is empty, and used in x + 0 is 0. And s = s "ab" builds a string starting from the empty one.

Numbers and strings

AWK has two kinds of values: numbers (double-precision floating point) and strings. A variable can hold either, and AWK converts as the operator requires:

  • Arithmetic (+ - * / % ^) converts operands to numbers. A string is converted by reading the longest numeric prefix: "12abc" is 12, "abc" is 0, " 3.5 " is 3.5.
  • Concatenation (writing two values next to each other) converts operands to strings. Numbers become their text form (3.0 becomes 3, and non-integers use the CONVFMT format, %.6g by default).
Bash Conversions in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Notice 1 + 2 " apples": arithmetic binds tighter than concatenation, so the result is 3 apples, not 12 apples. When in doubt, add parentheses. The number display follows %.6g, which is why 1/3 prints 0.333333, and very large numbers switch to exponent form in some awks (mawk prints 1e+10 for 100000 * 100000, gawk prints 10000000000). Use printf "%d" or %.0f when you need every digit.

Fields and comparison: strnum

Text read from input (fields, getline, command-line assignments, array elements created by split) that looks like a number is a "numeric string". It compares as a number against other numbers, and as a string otherwise. Constants in the program are never numeric strings: "10" is a string.

Bash How comparisons choose their type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The rule of thumb: if both sides are numbers or numeric strings, compare numerically; otherwise compare as strings. That is why $1 > $2 is true for 10 and 9 but $1 > "9" is false (a string constant forces a string comparison, and 1 sorts before 9). In the last line, 10 > 9 is true, but "10" > "9" and "10" > 9 are both false: a string on either side makes it a string comparison. When you need a specific behaviour, force it with +0 for numbers or "" for strings.

Arithmetic and assignment operators

Operator Meaning Example
+ - * / add, subtract, multiply, divide $2 * $3
% remainder (can be fractional) NR % 2
^ or ** power 2 ^ 8
- + (unary) negate, convert to number +$1
++ -- increment, decrement (prefix or postfix) n++, --k
= += -= *= /= %= ^= assignment sum += $3

Division is always floating point: 7 / 2 is 3.5. Use int(7 / 2) for integer division. x++ yields the old value and ++x the new one.

Bash Operators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Points to notice: ^ is right associative, so 2 ^ 3 ^ 2 is 2 ^ 9 = 512. Unary minus binds lower than ^, so -2 ^ 2 is -(2^2) = -4. A hexadecimal 0x10 or leading-zero 011 literal is not portable: depending on the awk, you may see 16, 0 or 10 and 11. Avoid them in portable programs.

Comparison, match and logical operators

Comparison: < <= == != >= >. Match: ~ and !~. Logical: &&, ||, !. These return 1 for true and 0 for false. The ternary operator cond ? a : b picks a value. The in operator tests array keys (covered with arrays). Boolean results are plain numbers, so you can count with them:

Bash Booleans are numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

String concatenation

There is no operator: placing two expressions side by side concatenates them. It has lower precedence than arithmetic and higher than comparison, which sometimes bites:

Bash Concatenation and precedence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

print a b == "xy" may surprise you: in some awks the comparison is parsed first. Always parenthesize a comparison inside print, and use parentheses around arithmetic mixed with concatenation. A bare > inside a print statement is even treated as an output redirection (see printf and output), so write print (a > b).

Operator precedence (high to low)

TEXT
( )                 grouping
$                   field reference
++ --               increment / decrement
^ **                exponentiation (right associative)
! + -               unary not, plus, minus
* / %               multiply, divide, remainder
+ -                 add, subtract
(concatenation)     string join
< <= != == > >=     comparison (non-associative)
~ !~                match
in                  array membership
&&                  and
||                  or
?:                  conditional (right associative)
= += -= *= /= %= ^=  assignment (right associative)

Passing values from the shell

There are three ways, from safest to riskiest:

  1. -v name=value assigns before BEGIN runs. Escape sequences in the value (like \n) are processed.
  2. name=value as an argument between file names: assigned when AWK gets to it, so it is visible after the previous file is read.
  3. ENVIRON["NAME"] reads an environment variable.

Putting shell variables inside the program text (with double quotes) is a bad habit: it mixes the data with the code and breaks on quotes.

Bash -v, command-line assignments and ENVIRON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Because -v hands AWK a value, not code, it is the safe way to put user input in a program. There is also ARGC and ARGV, the arguments to awk itself, which you can inspect and even modify in BEGIN.

Bash ARGV

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Built-in variables at a glance

Variable Meaning Default
FS, OFS input/output field separator blank, space
RS, ORS input/output record separator newline
NF, NR, FNR fields, record number, record number in this file
FILENAME current file
SUBSEP array subscript joiner \034
CONVFMT, OFMT number-to-string formats %.6g
ENVIRON, ARGC, ARGV environment and arguments
RSTART, RLENGTH set by match()

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the result of "3x" + 1 in AWK?

    A string is converted to a number by reading its numeric prefix: 3x becomes 3.
  2. 2. What does print 1 + 2 " apples" print?

    Arithmetic binds tighter than concatenation, so 1 + 2 is computed first.
  3. 3. Which is the safe way to pass a shell variable $v into an AWK program?

    -v assigns the value to an AWK variable without mixing it into the program text.
  4. 4. What is -2 ^ 2 in AWK?

    ^ has higher precedence than the unary minus, so this is -(2^2).
  5. 5. What does an uninitialized variable evaluate to?

    That's why count++ works without declaring count.

Summary

  • Variables need no declaration; unset means 0 or "".
  • Operators decide the conversion: arithmetic makes numbers, concatenation makes strings; input that looks numeric compares as a number.
  • Know the precedence: ^ before unary minus, arithmetic before concatenation, concatenation before comparison.
  • Booleans are 1 and 0; use parentheses around comparisons in print.
  • Pass data with -v, name=value arguments or ENVIRON, never by pasting into the program.

Next: print, printf and Output Redirection