ENVIRON

Modern versions of new awk (gawk, mawk, Bell Labs awk, any POSIX awk) all provide an array named ENVIRON. The array is indexed by environment variable name; the value is that variable's value. For instance, ENVIRON["HOME"] might be "/home/chris". To print out all the names and values, use a simple loop:

        for (i in ENVIRON)
                printf("ENVIRON['%s'] = '%s'\n", i, ENVIRON[i])

What if my awk doesn't have ENVIRON[]?

Short answer, get a better awk. There are many freely available versions.

Longer answer, on Unix you can use a pipe from the `env' or `printenv' commands, but this is less pretty, and may be a problem if the values contain newlines:

        # test this on your system before you depend on it!
        while ( ("env" | getline line) >0 )
        {
                varname=line
                varvalue=line
                sub(/=.*$/,"",varname)
                sub(/^[^=]*=/,"",varvalue)
                print "var [" varname "]='" varvalue "'"
        }