Next: , Up: Environment Variables   [Contents][Index]


2.5.1 The AWKPATH Environment Variable

In most 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 -f or -i options does not contain a directory separator ‘/’, 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 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 -i or -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 --traditional and --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 gawk would 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 ‘.:/usr/local/share/awk’.14 Since . 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 AWKPATH.

See section Shell Startup Files, for information on functions that help to manipulate the AWKPATH variable.

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 awk program.

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 AWKPATH.


Footnotes

(13)

Semicolons on MS-Windows.

(14)

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 $(datadir) generated when gawk was configured. You probably don’t need to worry about this, though.


Next: , Up: Environment Variables   [Contents][Index]