accessing shell variables from within awk?

Guy Harris guy at gorodish.Sun.COM
Tue Sep 20 13:32:31 AEST 1988


> 	How can I access a shell variable (to wit, a few environment
> variables) from within "awk"? I've tried various combinations of
> shell quoting, and whatnot, to no avail. Does any version of awk
> have a shortcut to get at it's environment?

Well, the "new awk" might, but I don't know how many UNIX systems offer it yet.
"awk" doesn't have any mechanism for getting at the UNIX environment (i.e., the
thing that "export" in the Bourne and Korn shells, and "setenv" in the C shell,
affect).

You can put assignments on the command line:

     "awk" variables may be set on the command line using arguments
     of  the  form  "variable=value".  This sets the "awk" variable
     "variable" to "value"  before  the  first  record  of  the  next
     filename argument is read.

which might be a way to pass them to "awk", e.g.

	awk -f awkfile foo="$foo"

which sets the "awk" variable "foo" to the value of the shell variable "foo".

> BTW, I'm running SunOS 3.5, if it matters much.

Not a lot; that "awk" is basically the S5R2 "awk", but the feature in question
is, I think, in the 4BSD "awk" as well.



More information about the Comp.unix.questions mailing list