Go to the previous section.

 

Invocation of awk

There are two ways to run awk: with an explicit program, or with one or more program files. Here are templates for both of them; items enclosed in `[...]' in these templates are optional.
awk [-Ffs] [-v var=val] [-V] [-C] [-c] [-a] [-e] [--] 'program' file ...
awk [-Ffs] -f source-file [-f source-file ...] [-v var=val] [-V] [-C] [-c] [-a] [-e] [--] file ...

Command Line Options

Options begin with a minus sign, and consist of a single character. The options and their meanings are as follows:
-Ffs
Sets the FS variable to fs (see section Specifying How Fields Are Separated).
-f source-file
Indicates that the awk program is to be found in source-file instead of in the first non-option argument.

-v var=val
Sets the variable var to the value val before execution of the program begins. Such variable values are available inside the BEGIN rule (see below for a fuller explanation).

The `-v' option only has room to set one variable, but you can use it more than once, setting another variable each time, like this: `-v foo=1 -v bar=2'.

-a
Specifies use of traditional awk syntax for regular expressions. This means that `\' can be used to quote any regular expression operators inside of square brackets, just as it can be outside of them. This mode is currently the default; the `-a' option is useful in shell scripts so that they will not break if the default is changed. See section Regular Expression Operators.
-e
Specifies use of egrep syntax for regular expressions. This means that `\' does not serve as a quoting character inside of square brackets; ideosyncratic techniques are needed to include various special characters within them. This mode may become the default at some time in the future. See section Regular Expression Operators.

-c
Specifies compatibility mode, in which the GNU extensions in gawk are disabled, so that gawk behaves just like Unix awk. These extensions are noted below, where their usage is explained. See section Downwards Compatibility and Debugging.

-V
Prints version information for this particular copy of gawk. This is so you can determine if your copy of gawk is up to date with respect to whatever the Free Software Foundation is currently distributing. This option may disappear in a future version of gawk.

-C
Prints the short version of the General Public License. This option may disappear in a future version of gawk.
--
Signals the end of the command line options. The following arguments are not treated as options even if they begin with `-'. This interpretation of `--' follows the POSIX argument parsing conventions.

This is useful if you have file names that start with `-', or in shell scripts, if you have file names that will be specified by the user and that might start with `-'.

Any other options are flagged as invalid with a warning message, but are otherwise ignored.

In compatibility mode, as a special case, if the value of fs supplied to the `-F' option is `t', then FS is set to the tab character ("\t"). Also, the `-C' and `-V' options are not recognized.

 If the `-f' option is not used, then the first non-option command line argument is expected to be the program text.

The `-f' option may be used more than once on the command line. Then awk reads its program source from all of the named files, as if they had been concatenated together into one big file. This is useful for creating libraries of awk functions. Useful functions can be written once, and then retrieved from a standard place, instead of having to be included into each individual program. You can still type in a program at the terminal and use library functions, by specifying `-f /dev/tty'. awk will read a file from the terminal to use as part of the awk program. After typing your program, type Control-d (the end-of-file character) to terminate it.

Other Command Line Arguments

Any additional arguments on the command line are normally treated as input files to be processed in the order specified. However, an argument that has the form var=value, means to assign the value value to the variable var---it does not specify a file at all. 

All these arguments are made available to your awk program in the ARGV array (see section Built-in Variables). Command line options and the program text (if present) are omitted from the ARGV array. All other arguments, including variable assignments, are included.

The distinction between file name arguments and variable-assignment arguments is made when awk is about to open the next input file. At that point in execution, it checks the "file name" to see whether it is really a variable assignment; if so, awk sets the variable instead of reading a file.

Therefore, the variables actually receive the specified values after all previously specified files have been read. In particular, the values of variables assigned in this fashion are not available inside a BEGIN rule (see section BEGIN and END Special Patterns), since such rules are run before awk begins scanning the argument list.

 In some earlier implementations of awk, when a variable assignment occurred before any file names, the assignment would happen before the BEGIN rule was executed. Some applications came to depend upon this "feature". When awk was changed to be more consistent, the `-v' option was added to accomodate applications that depended upon this old behaviour.

The variable assignment feature is most useful for assigning to variables such as RS, OFS, and ORS, which control input and output formats, before scanning the data files. It is also useful for controlling state if multiple passes are needed over a data file. For example: 

awk 'pass == 1  { pass 1 stuff }
     pass == 2  { pass 2 stuff }' pass=1 datafile pass=2 datafile

The AWKPATH Environment Variable

The previous section described how awk program files can be named on the command line with the `-f' option. In some awk implementations, you must supply a precise path name for each program file, unless the file is in the current directory.

But in gawk, if the file name supplied in the `-f' option does not contain a `/', then gawk searches a list of directories (called the search path), one by one, looking for a file with the specified name.

The search path is actually a string containing directory names separated by colons. gawk gets its search path from the AWKPATH environment variable. If that variable does not exist, gawk uses the default path, which is `.:/usr/lib/awk:/usr/local/lib/awk'.

 The search path feature is particularly useful for building up libraries of useful awk functions. The library files can be placed in a standard directory that is in the default path, and then specified on the command line with a short file name. Otherwise, the full file name would have to be typed for each file.

Path searching is not done if gawk is in compatibility mode. See section Invocation of awk.

Note: if you want files in the current directory to be found, you must include the current directory in the path, either by writing `.' as an entry in the path, or by writing a null entry in the path. (A null entry is indicated by starting or ending the path with a colon, or by placing two colons next to each other (`::').) If the current directory is not included in the path, then files cannot be found in the current directory. This path search mechanism is identical to the shell's.

Go to the previous section.