Next: , Previous: User-modified, Up: Built-in Variables   [Contents][Index]

7.5.2 Built-in Variables That Convey Information

The following is an alphabetical list of variables that awk sets automatically on certain occasions in order to provide information to your program.

The variables that are specific to gawk are marked with a pound sign (‘#’). These variables are gawk extensions. In other awk implementations or if gawk is in compatibility mode (see Options), they are not special:


The command-line arguments available to awk programs are stored in an array called ARGV. ARGC is the number of command-line arguments present. See Other Arguments. Unlike most awk arrays, ARGV is indexed from 0 to ARGC - 1. In the following example:

$ awk 'BEGIN {
>         for (i = 0; i < ARGC; i++)
>             print ARGV[i]
>      }' inventory-shipped mail-list
-| awk
-| inventory-shipped
-| mail-list

ARGV[0] contains ‘awk’, ARGV[1] contains ‘inventory-shipped’, and ARGV[2] contains ‘mail-list’. The value of ARGC is three, one more than the index of the last element in ARGV, because the elements are numbered from zero.

The names ARGC and ARGV, as well as the convention of indexing the array from 0 to ARGC - 1, are derived from the C language’s method of accessing command-line arguments.

The value of ARGV[0] can vary from system to system. Also, you should note that the program text is not included in ARGV, nor are any of awk’s command-line options. See ARGC and ARGV, for information about how awk uses these variables. (d.c.)


The index in ARGV of the current file being processed. Every time gawk opens a new data file for processing, it sets ARGIND to the index in ARGV of the file name. When gawk is processing the input files, ‘FILENAME == ARGV[ARGIND]’ is always true.

This variable is useful in file processing; it allows you to tell how far along you are in the list of data files as well as to distinguish between successive instances of the same file name on the command line.

While you can change the value of ARGIND within your awk program, gawk automatically sets it to a new value when it opens the next file.


An associative array containing the values of the environment. The array indices are the environment variable names; the elements are the values of the particular environment variables. For example, ENVIRON["HOME"] might be "/home/arnold". Changing this array does not affect the environment passed on to any programs that awk may spawn via redirection or the system() function. (In a future version of gawk, it may do so.)

Some operating systems may not have environment variables. On such systems, the ENVIRON array is empty (except for ENVIRON["AWKPATH"] and ENVIRON["AWKLIBPATH"]; see AWKPATH Variable, and see AWKLIBPATH Variable).


If a system error occurs during a redirection for getline, during a read for getline, or during a close() operation, then ERRNO contains a string describing the error.

In addition, gawk clears ERRNO before opening each command-line input file. This enables checking if the file is readable inside a BEGINFILE pattern (see BEGINFILE/ENDFILE).

Otherwise, ERRNO works similarly to the C variable errno. Except for the case just mentioned, gawk never clears it (sets it to zero or ""). Thus, you should only expect its value to be meaningful when an I/O operation returns a failure value, such as getline returning -1. You are, of course, free to clear it yourself before doing an I/O operation.


The name of the current input file. When no data files are listed on the command line, awk reads from the standard input and FILENAME is set to "-". FILENAME changes each time a new file is read (see Reading Files). Inside a BEGIN rule, the value of FILENAME is "", because there are no input files being processed yet.38 (d.c.) Note, though, that using getline (see Getline) inside a BEGIN rule can give FILENAME a value.


The current record number in the current file. awk increments FNR each time it reads a new record (see Records). awk resets FNR to zero each time it starts a new input file.


The number of fields in the current input record. NF is set each time a new record is read, when a new field is created, or when $0 changes (see Fields).

Unlike most of the variables described in this subsection, assigning a value to NF has the potential to affect awk’s internal workings. In particular, assignments to NF can be used to create fields in or remove fields from the current record. See Changing Fields.


An array whose indices and corresponding values are the names of all the built-in, user-defined, and extension functions in the program.

NOTE: Attempting to use the delete statement with the FUNCTAB array causes a fatal error. Any attempt to assign to an element of FUNCTAB also causes a fatal error.


The number of input records awk has processed since the beginning of the program’s execution (see Records). awk increments NR each time it reads a new record.


The elements of this array provide access to information about the running awk program. The following elements (listed alphabetically) are guaranteed to be available:


The value of the getegid() system call.


The value of the geteuid() system call.


This is "FS" if field splitting with FS is in effect, "FIELDWIDTHS" if field splitting with FIELDWIDTHS is in effect, or "FPAT" if field matching with FPAT is in effect.


A subarray, indexed by the names of all identifiers used in the text of the awk program. An identifier is simply the name of a variable (be it scalar or array), built-in function, user-defined function, or extension function. For each identifier, the value of the element is one of the following:


The identifier is an array.


The identifier is a built-in function.


The identifier is an extension function loaded via @load or -l.


The identifier is a scalar.


The identifier is untyped (could be used as a scalar or an array; gawk doesn’t know yet).


The identifier is a user-defined function.

The values indicate what gawk knows about the identifiers after it has finished parsing the program; they are not updated while the program runs.


The value of the getgid() system call.


The process group ID of the current process.


The process ID of the current process.


The parent process ID of the current process.


If this element exists in PROCINFO, its value controls the order in which array indices will be processed by ‘for (indx in array)’ loops. This is an advanced feature, so we defer the full description until later; see Scanning an Array.


The default time format string for strftime(). Assigning a new value to this element changes the default. See Time Functions.


The value of the getuid() system call.


The version of gawk.

The following additional elements in the array are available to provide information about the MPFR and GMP libraries if your version of gawk supports arbitrary-precision arithmetic (see Arbitrary Precision Arithmetic):


The version of the GNU MPFR library.


The version of the GNU MP library.


The maximum precision supported by MPFR.


The minimum precision required by MPFR.

The following additional elements in the array are available to provide information about the version of the extension API, if your version of gawk supports dynamic loading of extension functions (see Dynamic Extensions):


The major version of the extension API.


The minor version of the extension API.

On some systems, there may be elements in the array, "group1" through "groupN" for some N. N is the number of supplementary groups that the process has. Use the in operator to test for these elements (see Reference to Elements).

The PROCINFO array has the following additional uses:


The length of the substring matched by the match() function (see String Functions). RLENGTH is set by invoking the match() function. Its value is the length of the matched string, or -1 if no match is found.


The start index in characters of the substring that is matched by the match() function (see String Functions). RSTART is set by invoking the match() function. Its value is the position of the string where the matched substring starts, or zero if no match was found.

RT #

The input text that matched the text denoted by RS, the record separator. It is set every time a record is read.


An array whose indices are the names of all defined global variables and arrays in the program. SYMTAB makes gawk’s symbol table visible to the awk programmer. It is built as gawk parses the program and is complete before the program starts to run.

The array may be used for indirect access to read or write the value of a variable:

foo = 5
SYMTAB["foo"] = 4
print foo    # prints 4

The isarray() function (see Type Functions) may be used to test if an element in SYMTAB is an array. Also, you may not use the delete statement with the SYMTAB array.

You may use an index for SYMTAB that is not a predefined identifier:

SYMTAB["xxx"] = 5
print SYMTAB["xxx"]

This works as expected: in this case SYMTAB acts just like a regular array. The only difference is that you can’t then delete SYMTAB["xxx"].

The SYMTAB array is more interesting than it looks. Andrew Schorr points out that it effectively gives awk data pointers. Consider his example:

# Indirect multiply of any variable by amount, return result

function multiply(variable, amount)
    return SYMTAB[variable] *= amount

NOTE: In order to avoid severe time-travel paradoxes,39 neither FUNCTAB nor SYMTAB is available as an element within the SYMTAB array.

Changing NR and FNR

awk increments NR and FNR each time it reads a record, instead of setting them to the absolute value of the number of records read. This means that a program can change these variables and their new values are incremented for each record. (d.c.) The following example shows this:

$ echo '1
> 2
> 3
> 4' | awk 'NR == 2 { NR = 17 }
> { print NR }'
-| 1
-| 17
-| 18
-| 19

Before FNR was added to the awk language (see V7/SVR3.1), many awk programs used this feature to track the number of records in a file by resetting NR to zero when FILENAME changed.



Some early implementations of Unix awk initialized FILENAME to "-", even if there were data files to be processed. This behavior was incorrect and should not be relied upon in your programs.


Not to mention difficult implementation issues.

Next: , Previous: User-modified, Up: Built-in Variables   [Contents][Index]