Go to the first, previous, next, last section, table of contents.
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, assigns
the value value to the variable var---it does not specify a
file at all.
(This was discussed earlier in
section Assigning Variables on the Command Line.)
All these arguments are made available to your @command{awk} program in the
ARGV array (see section Built-in Variables). Command-line options
and the program text (if present) are omitted from ARGV.
All other arguments, including variable assignments, are
included. As each element of ARGV is processed, @command{gawk}
sets the variable ARGIND to the index in ARGV of the
current element.
The distinction between file name arguments and variable-assignment arguments is made when @command{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, @command{awk} sets the variable instead of reading a file.
Therefore, the variables actually receive the given 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 The BEGIN and END Special Patterns),
because such rules are run before @command{awk} begins scanning the argument list.
The variable values given on the command-line are processed for escape sequences (see section Escape Sequences). (d.c.)
In some earlier implementations of @command{awk}, when a variable assignment
occurred before any file names, the assignment would happen before
the BEGIN rule was executed. @command{awk}'s behavior was thus
inconsistent; some command-line assignments were available inside the
BEGIN rule, while others were not. Unfortunately,
some applications came to depend
upon this "feature." When @command{awk} was changed to be more consistent,
the @option{-v} option was added to accommodate applications that depended
upon the old behavior.
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 mydata pass=2 mydata
Given the variable assignment feature, the @option{-F} option for setting
the value of FS is not
strictly necessary. It remains for historical compatibility.
Go to the first, previous, next, last section, table of contents.