Next: , Up: Environment Variables

2.5.1 The AWKPATH Environment Variable

In most 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 to the -f or -i options 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 a string consisting of directory names separated by colons. gawk gets its search path from the AWKPATH environment variable. If that variable does not exist, gawk uses a default path, ‘.:/usr/local/share/awk’.1

The search path feature is particularly useful 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, the full file name would have to be typed for each file.

By using the -i option, or the --source and -f options, your command-line awk programs can use facilities in awk library files (see Library Functions). Path searching is not done if gawk is in compatibility mode. This is true for both --traditional and --posix. See Options.

If the source code is not found after the initial search, the path is searched again after adding the default ‘.awk’ suffix to the filename.

NOTE: To include the current directory in the path, either place . explicitly in the path or write 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 (‘::’).) This path search mechanism is similar to the shell's.

However, gawk always looks in the current directory before searching AWKPATH, so there is no real reason to include the current directory in the search path.

If AWKPATH is not defined in the environment, gawk places its default search path into ENVIRON["AWKPATH"]. This makes it easy to determine the actual search path that gawk will use from within an awk program.

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


[1] 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.