accessing shell variables from within awk?

Betsy Dunphy pdvbld at prcpto.UUCP
Tue Sep 27 05:38:57 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.......

As I understand the question, the writer desires to be able to
use environmental variables in awk functions like printf.  The following
example prints file paths: 

        ls | awk '{printf("%s/%s\n","'$HOME'",$1)}'


$HOME is expanded by the shell by the notation of the single-quotes.  The
trick is the double-quotes - these denote the result as a string.  Otherwise,
the value of $HOME would be treated as a variable (an unitialized variable).

This works in both bourne shell and csh (of course! :-)) and was tried on
both a Masscomp 5500 3.1.B RTU and SUN 3/160 under 3.2.


---------------------------------------------------------------------
Betsy Dunphy            |
PRC                     |
600 West Services Road  |  The usual disclaimer here......
Washington, DC 20041    |
703-883-5138            |
prcpto at pdvbld.UUCP      |
---------------------------------------------------------------------



More information about the Comp.unix.questions mailing list