AWKPATH Variable (The GNU Awk User’s Guide)
2.5.1 The AWKPATH Environment Variable
awk implementations, you must supply a precise pathname for each program file, unless the file is in the current directory. But with
gawk, if the file name supplied to the
-i options does not contain a directory separator ‘
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 a string consisting of directory names separated by colons.13
gawk gets its search path from the
AWKPATH environment variable. If that variable does not exist, or if it has an empty value,
gawk uses a default path (described shortly).
The search path feature is particularly helpful for building libraries of useful
awk functions. The library files can be placed in a standard directory in the default path and then specified on the command line with a short file name. Otherwise, you would have to type the full file name for each file.
By using the
-f options, your command-line
awk programs can use facilities in
awk library files (see section A Library of awk Functions). Path searching is not done if
gawk is in compatibility mode. This is true for both
--posix. See section Command-Line Options.
If the source code file is not found after the initial search, the path is searched again after adding the suffix ‘
.awk’ to the file name.
gawk’s path search mechanism is similar to the shell’s. (See The Bourne-Again SHell manual.) It treats a null entry in the path as indicating the current directory. (A null entry is indicated by starting or ending the path with a colon or by placing two colons next to each other [‘
NOTE: To include the current directory in the path, either place
.as an entry in the path or write a null entry in the path.
Different past versions of
gawkwould also look explicitly in the current directory, either before or after the path search. As of version 4.1.2, this no longer happens; if you wish to look in the current directory, you must include
.either as a separate entry or as a null entry in the search path.
The default value for
AWKPATH is ‘
. is included at the beginning,
gawk searches first in the current directory and then in
/usr/local/share/awk. In practice, this means that you will rarely need to change the value of
See section Shell Startup Files, for information on functions that help to manipulate the
gawk places the value of the search path that it used into
ENVIRON["AWKPATH"]. This provides access to the actual search path value from within an
Although you can change
ENVIRON["AWKPATH"] within your
awk program, this has no effect on the running program’s behavior. This makes sense: the
AWKPATH environment variable is used to find the program source files. Once your program is running, all the files have been found, and
gawk no longer needs to use
Semicolons on MS-Windows.
Your version of
gawk may use a different directory; it will depend upon how
gawk was built and installed. The actual directory is the value of
$(pkgdatadir) generated when
gawk was configured. (For more detail, see the
INSTALL file in the source distribution, and see Compiling gawk for Unix-Like Systems. You probably don’t need to worry about this, though.)