Options begin with a dash and consist of a single character. GNU-style long options consist of two dashes and a keyword. The keyword can be abbreviated, as long as the abbreviation allows the option to be uniquely identified. If the option takes an argument, either the keyword is immediately followed by an equals sign (‘=’) and the argument’s value, or the keyword and the argument’s value are separated by whitespace. If a particular option with a value is given more than once, it is the last value that counts.
Each long option for
gawk has a corresponding
POSIX-style short option.
The long and short options are
interchangeable in all contexts.
The following list describes options mandated by the POSIX standard:
FS variable to fs
(see Field Separators).
awk program source from source-file
instead of in the first nonoption argument.
This option may be given multiple times; the
program consists of the concatenation of the contents of
each specified source-file.
Set the variable var to the value val before
execution of the program begins. Such variable values are available
(see Other Arguments).
The -v option can only set one variable, but it can be used more than once, setting another variable each time, like this: ‘awk -v foo=1 -v bar=2 …’.
CAUTION: Using -v to set the values of the built-in variables may lead to surprising results.
awkwill reset the values of those variables as it needs to, possibly ignoring any initial value you may have given.
Provide an implementation-specific option.
This is the POSIX convention for providing implementation-specific options.
also have corresponding GNU-style long options.
Note that the long options may be abbreviated, as long as
the abbreviations remain unique.
The full list of
gawk-specific options is provided next.
Signal 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 that could start with ‘-’.
It is also useful for passing options on to the
program; see Getopt Function.
The following list describes
gawk to treat all input data as single-byte characters.
In addition, all output written with
is treated as single-byte characters.
gawk follows the POSIX standard and attempts to process
its input data according to the current locale (see Locales). This can often involve
converting multibyte characters into wide characters (internally), and
can lead to problems or confusion if the input data does not contain valid
multibyte characters. This option is an easy way to tell
“Hands off my data!”
Specify compatibility mode, in which the GNU extensions to
awk language are disabled, so that
gawk behaves just
which summarizes the extensions.
Print the short version of the General Public License and then exit.
Print a sorted list of global variables, their types, and final values to file. If no file is provided, print this list to a file named awkvars.out in the current directory. No space is allowed between the -d and file, if file is supplied.
Having a list of all global variables is a good way to look for
typographical errors in your programs.
You would also use this option if you have a large program with a lot of
functions, and you want to be sure that your functions don’t
inadvertently use global variables that you meant to be local.
(This is a particularly easy mistake to make with simple variable
Enable debugging of
By default, the debugger reads commands interactively from the keyboard
The optional file argument allows you to specify a file with a list
of commands for the debugger to execute noninteractively.
No space is allowed between the -D and file, if
file is supplied.
Provide program source code in the program-text. This option allows you to mix source code in files with source code that you enter on the command line. This is particularly useful when you have library functions that you want to use from your command-line programs (see AWKPATH Variable).
Similar to -f, read
awk program text from file.
There are two differences from -f:
This option is particularly necessary for World Wide Web CGI applications
that pass arguments through the URL; using this option prevents a malicious
(or other) user from passing in options, assignments, or
code (via -e) to the CGI application.11
This option should be used
with ‘#!’ scripts (see Executable Scripts), like so:
#! /usr/local/bin/gawk -E awk program here …
Analyze the source program and
generate a GNU
gettext portable object template file on standard
output for all string constants that have been marked for translation.
for information about this option.
Print a “usage” message summarizing the short- and long-style options
gawk accepts and then exit.
awk source library from source-file. This option
is completely equivalent to using the
@include directive inside
your program. It is very similar to the -f option,
but there are two important differences. First, when -i is
used, the program source is not loaded if it has been previously
loaded, whereas with -f,
gawk always loads the file.
Second, because this option is intended to be used with code libraries,
gawk does not recognize such files as constituting main program
input. Thus, after processing an -i argument,
still expects to find the main source code via the -f option
or on the command line.
Load a dynamic extension named ext. Extensions
are stored as system shared libraries.
This option searches for the library using the
environment variable. The correct library suffix for your platform will be
supplied by default, so it need not be specified in the extension name.
The extension initialization routine should be named
An alternative is to use the
@load keyword inside the program to load
a shared library. This advanced feature is described in detail in Dynamic Extensions.
Warn about constructs that are dubious or nonportable to
No space is allowed between the -L and value, if
value is supplied.
Some warnings are issued when
gawk first reads your program. Others
are issued at runtime, as your program executes.
With an optional argument of ‘fatal’,
lint warnings become fatal errors.
This may be drastic, but its use will certainly encourage the
development of cleaner
With an optional argument of ‘invalid’, only warnings about things
that are actually invalid are issued. (This is not fully implemented yet.)
Some warnings are only printed once, even if the dubious constructs they
warn about occur multiple times in your
awk program. Thus,
when eliminating problems pointed out by --lint, you should take
care to search for all occurrences of each inappropriate construct. As
awk programs are usually short, doing so is not burdensome.
Select arbitrary-precision arithmetic on numbers. This option has no effect
gawk is not compiled to use the GNU MPFR and MP libraries
(see Arbitrary Precision Arithmetic).
Enable automatic interpretation of octal and hexadecimal values in input data (see Nondecimal Data).
CAUTION: This option can severely break old programs. Use with care. Also note that this option may disappear in a future version of
Force the use of the locale’s decimal point character when parsing numeric input data (see Locales).
Enable pretty-printing of
By default, the output program is created in a file named awkprof.out
The optional file argument allows you to specify a different
file name for the output.
No space is allowed between the -o and file, if
file is supplied.
NOTE: Due to the way
gawkhas evolved, with this option your program still executes. This will change in the next major release, such that
gawkwill only pretty-print the program and not run it.
Enable some optimizations on the internal representation of the program. At the moment, this includes just simple constant folding.
Enable profiling of
By default, profiles are created in a file named awkprof.out.
The optional file argument allows you to specify a different
file name for the profile file.
No space is allowed between the -p and file, if
file is supplied.
The profile contains execution counts for each statement in the program in the left margin, and function call counts for each function.
Operate in strict POSIX mode. This disables all
extensions (just like --traditional) and
disables all extensions not allowed by POSIX.
See Common Extensions for a summary of the extensions
gawk that are disabled by this option.
the following additional
FSis equal to a single space (see Fields).
FSto be a single TAB character (see Field Separators).
If you supply both --traditional and --posix on the
command line, --posix takes precedence.
issues a warning if both options are supplied.
Allow interval expressions
(see Regexp Operators)
This is now
gawk’s default behavior.
Nevertheless, this option remains (both for backward compatibility
and for use in combination with --traditional).
input redirections with
output redirections with
and dynamic extensions.
This is particularly useful when you want to run
from questionable sources and need to make sure the scripts
can’t access your system (other than the specified input data file).
Warn about constructs that are not available in the original version of
awk from Version 7 Unix
Print version information for this particular copy of
This allows you to determine if your copy of
gawk is up to date
with respect to whatever the Free Software Foundation is currently
It is also useful for bug reports
As long as program text has been supplied, 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
"\t"). This is true only for --traditional and not
(see Field Separators).
The -f option may be used more than once on the command line.
If it is,
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. These functions
can be written once and then retrieved from a standard place, instead
of having to be included in each individual program.
The -i option is similar in this regard.
(As mentioned in
function names must be unique.)
awk, library functions can still be used, even
if the program is entered at the keyboard,
by specifying ‘-f /dev/tty’. After typing your program,
type Ctrl-d (the end-of-file character) to terminate it.
(You may also use ‘-f -’ to read program source from the standard
input, but then you will not be able to also use the standard input as a
source of data.)
Because it is clumsy using the standard
awk mechanisms to mix
source file and command-line
provides the -e option. This does not require you to
preempt the standard input for your source code; it allows you to easily
mix command-line and library source code (see AWKPATH Variable).
As with -f, the -e and -i
options may also be used multiple times on the command line.
If no -f or -e option is specified, then
uses the first nonoption command-line argument as the text of the
program source code.
If the environment variable
gawk behaves in strict POSIX mode, exactly as if
you had supplied --posix.
Many GNU programs look for this environment variable to suppress
extensions that conflict with POSIX, but
differently: it suppresses all extensions, even those that do not
conflict with POSIX, and behaves in
strict POSIX mode. If --lint is supplied on the command line
gawk turns on POSIX mode because of
then it issues a warning message indicating that POSIX
mode is in effect.
You would typically set this variable in your shell’s startup file.
For a Bourne-compatible shell (such as Bash), you would add these
lines to the .profile file in your home directory:
POSIXLY_CORRECT=true export POSIXLY_CORRECT
For a C shell-compatible shell,12 you would add this line to the .login file in your home directory:
setenv POSIXLY_CORRECT true
POSIXLY_CORRECT set is not recommended for daily use,
but it is good for testing the portability of your programs to other
For more detail,
please see Section 4.4 of RFC 3875. Also see the
explanatory note sent to the