[Discuss] awk linux and ENVIRON

p.willis at telus.net p.willis at telus.net
Fri Jun 23 14:19:51 PDT 2006


Quoting "Alan W. Irwin" <irwin at beluga.phys.uvic.ca>:

> 
> Of course, under Linux to find out all environment variables simply run the
> printenv command from the shell.  From my awk man page, all those
> environment variables should be automatically indexed by the ENVIRON
> dictionary under awk. Whether that actually works for awk might depend on
> the awk implementation (there are several including nawk), the shell, etc.
> For example, from my awk man page it appears that the ENVIRON dictionary is
> an extension beyond POSIX so that implies some awk implementations won't
> have it.
> 
> It's been quite a while since I used awk so could you send a one-liner that
> prints out the full contents of the ENVIRON dictionary so I can test whether
> ENVIRON corresponds to the results of printenv on my awk/shell platform?
> Others who are completely unfamiliar with awk might want to participate in
> this test as well.
> 
> Alan

Hello,

I'm using gawk (GNU awk). The documentation states that ENVIRON should
provide environment variable access on systems that have them. 

I found the problem(s). There was an error in my awk usage.


awk requires an input to work

ie:

echo something | awk '{print $1}'

<returns> something

awk '{print "something"}'

<waits for input>




Hence, the following now works:

export A=1234; echo dummy | awk '{printf ENVIRON["A"]}'

<returns> 1234

Notice that I need an input into awk by piping some
data into it via:

echo dummy |

Another reason it wasn't working was, I also needed to 
use 'export' to generate the environment variable.
I forgot about this when working between csh(??solaris) and bash.

On solaris I didn't require 'export', I just typed:

A=123

or

set A=123

In summary, ENVIRON works fine, in awk under linux.
I just need to be sure which shell I'm using and 
remember to give awk data to work with.

List all your environment variables with:

env | awk -F "=" '{print $1}'

test for ENVIRON with:

env | awk -F "=" '{print $1}' | awk '{print ENVIRON[$1]}'



Peter




More information about the Discuss mailing list