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


Using Variables in a Program

Variables let you give names to values and refer to them later. You have already seen variables in many of the examples. The name of a variable must be a sequence of letters, digits and underscores, but it may not begin with a digit. Case is significant in variable names; a and A are distinct variables.

A variable name is a valid expression by itself; it represents the variable's current value. Variables are given new values with assignment operators, increment operators and decrement operators. See section Assignment Expressions.

A few variables have special built-in meanings, such as FS, the field separator, and NF, the number of fields in the current input record. See section Built-in Variables, for a list of them. These built-in variables can be used and assigned just like all other variables, but their values are also used or changed automatically by awk. All built-in variables names are entirely upper-case.

Variables in awk can be assigned either numeric or string values. By default, variables are initialized to the empty string, which is zero if converted to a number. There is no need to "initialize" each variable explicitly in awk, the way you would in C and in most other traditional languages.


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