Using shell variables

When it comes to automation and scripting, you'd often need to construct commands that can accept input from user, file, output of a shell command, etc. As mentioned before, this book assumes bash as the shell being used.

info As an example, see my repo ch: command help for a practical shell script, where commands are constructed dynamically.

-v option

The most common method is to use the -v command line option.

$ # assume that the 's' variable is part of some bash script
$ # or perhaps a variable that has stored the output of a shell command
$ s='cake'
$ awk -v word="$s" '$2==word' table.txt
blue cake mug shirt -7


To access environment variables of the shell, you can call the special array variable ENVIRON with the name of the environment variable as a string key.

$ # existing environment variable
$ # output shown here is for my machine, would differ for you
$ awk 'BEGIN{print ENVIRON["HOME"]}'
$ awk 'BEGIN{print ENVIRON["SHELL"]}'

$ # defined along with awk command
$ # note that the variable is placed before awk
$ word='hello' awk 'BEGIN{print ENVIRON["word"]}'

ENVIRON is a good way to get around awk's interpretation of escape sequences. This is especially helpful for fixed string matching, see index section for examples.

$ s='hi\nbye'

$ # when passed via -v option
$ awk -v ip="$s" 'BEGIN{print ip}'

$ # when passed as an environment variable
$ ip="$s" awk 'BEGIN{print ENVIRON["ip"]}'

Here's another example when a regexp is passed to an awk command.

$ # when passed via -v option
$ r='\Bpar\B'
$ awk -v rgx="$r" '$0 ~ rgx' word_anchors.txt
awk: warning: escape sequence `\B' treated as plain `B'
$ r='\\Bpar\\B'
$ awk -v rgx="$r" '$0 ~ rgx' word_anchors.txt
apparent effort
two spare computers

$ # when passed as an environment variable
$ r='\Bpar\B'
$ rgx="$r" awk '$0 ~ ENVIRON["rgx"]' word_anchors.txt
apparent effort
two spare computers


This short chapter revisited the -v command line option and introduced the ENVIRON special array. These are particularly useful when the awk command is part of a shell script. More about arrays will be discussed in later chapters.

The next chapter will cover control structures.


a) Use contents of s variable to display all matching lines from the input file sample.txt. Assume that the s variable doesn't have any regexp metacharacters and construct a solution such that only whole words are matched.

$ s='do'
$ ##### add your solution here
Just do-it

b) Replace all occurrences of o for the input file addr.txt with literal contents of s variable. Assume that the s variable has regexp metacharacters.

$ s='\&/'
$ ##### add your solution here
Hell\&/ W\&/rld
H\&/w are y\&/u
This game is g\&/\&/d
T\&/day is sunny
Y\&/u are funny