Bash Pass Shell Variables To Awk Script

How do I pass shell variables to awk command or script under Linux or UNIX-like operating systems? How do I use shell variables in an awk script? How can I allow awk to use shell variables?

There are two ways for passing command-line shell variables to an awk script. This page explains how to get shell variables into awk using various methods.

Bash Pass Shell Variables To Awk Using -v Option

The -v option can be used to pass shell variables to awk command. Consider the following simple example:

echo | awk -v r=$root '{ print "shell variable $root value is " r}'

It works with various version of awk running on Linux, *BSD, macOS, and Unix-like systems.

How do I use shell variables in an awk script?

The variable is available in the BEGIN block as well. For example:

awk -v var=$a 'BEGIN{ ans=var*2} { print ans}'<<</dev/null

How to pass multiple shell variables to awk

The syntax is as follows for passing multiple -v to the awk:

text="Total is : "
awk -v a=$x -v b=$y -v c="$text" 'BEGIN {ans=a+b; print c " " ans}'

Passing command line shell variables to awk on Linux and Unix-like systems

AWK and command substitution

Command substitution means nothing more but to run a shell command and store its output to a variable or display back using echo command. For example, display date and time using a shell variable:

echo "$today"

One can allow awk to use shell variables created with command substitution as follows:

## bash ##
awk -v today="$(date)" 'BEGIN {print today}'
## more portable ##
awk -v today="`date`" 'BEGIN {print "Today is " today}'

In this example search file using awk as follows:

# Usage : Search word using awk for given file.
# Syntax: ./script "word-to-search" fileToSearch
awk -v search="$s" '$0 ~ search' "$i"

Save and close the file. Run it as follows:
$ ./ "vivek" /etc/passwd
Sample outputs:

vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash

We can match a pattern given in a variable in awk and delete a UFW firewall rule:

sudo ufw show added | sudo awk -v myip="$ip" '$0 ~ myip{ gsub("ufw","ufw delete",$0); system($0) }'

How to access shell environment variables in awk using ENVIRON

You need to use ENVIRON shell variable. From the gnu/awk man page:

An array containing the values of the current environment. The array is indexed by the environment variables, each element being the value of that variable (e.g., ENVIRON[“HOME”] might be “/home/arnold”). Changing this array does not affect the environment seen by programs which gawk spawns via redirection or the system() function.

For example, print $BASH and $USER shell variables using awk:

echo "$BASH"
echo "$USER"
echo "$HOME"
export demo="Om nom nom"
awk 'BEGIN{print ENVIRON["HOME"]}'
awk 'BEGIN{print ENVIRON["demo"]}'
var="value" awk 'BEGIN{print ENVIRON["var"]}'
awk 'BEGIN{print "Hello, "ENVIRON["USER"], "! Home dir " ENVIRON["HOME"]}'

A note about ARGV

ARGV is array of command line arguments passed to the awk. The array is indexed from 0 to ARGC – 1. Dynamically changing the contents of ARGV can control the files used for data. Let us see how to use ARGV with awk and shell variables:

awk 'BEGIN {print ARGV[0]}' 
awk 'BEGIN {print ARGV[1]}' "$os"
awk 'BEGIN {print ARGV[1] " " ARGV[2]}' $a $b
awk 'BEGIN {total=ARGV[1] + ARGV[2]; print total}' $a $b

Setup shell variables using awk

You can use eval command as follows:

eval $(awk 'BEGIN{ print "vech=Bus"}' < /dev/null)
echo $vech

Sample outputs:


See also:

  1. How To Use awk In Bash Scripting

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • David May 18, 2012 @ 20:22

    You can pass a varable directly if delimited properly. For example:

      total_fields=`echo "$LINE" | awk -F/ '{print NF}'`
      history_file=`echo "$LINE" | awk -F/ '{print $NF}'`
      # Example to pass a variable in awk
      num_fields=`expr $total_fields - 1`
      user_dir=`echo $LINE |awk -F/ '{print $'''$num_fields'''}'`
      echo "$user_dir"

    Edited by Admin – added formatting to the code

    • James Kernaghan Jan 10, 2013 @ 15:28

      This has been breaking my heart for 12 hours – thank you! I was simply trying to run commands through iteration of a char-delimited text line, so was trying to figure out how to substitute in the position parameter, via a while loop:

      ix=1; while [ $ix -le `echo rpc,err,warn | awk -F, ‘{print NF}’` ]; do echo Huzzah `echo rpc,err,warn | awk -F”,” ‘{ print $”’$ix”’ }’`; ix=`expr $ix + 1`; done

      In that case: rpc,err,warn is an example of the string I’m trying to iterate against; I’ll ultimately grep each word from a file.

      Can you explain what allows the triple ”’$num_fields”’ to substitute a foreign var in for the position param?

      Thanks a million!

      • Goal Lin Jan 17, 2013 @ 8:06

        I suggest you do this using three loops, each for rpc, err and warn

  • David May 18, 2012 @ 20:24

    Works well once you have the format

  • prats Sep 25, 2013 @ 10:31

    Thanks -v works :)

  • volkan Jul 15, 2015 @ 9:17

    Thank you.

  • simgirl Nov 5, 2015 @ 17:55

    Bless you

  • l10nn Apr 2, 2016 @ 0:12

    Great information!

    Thanks a lot

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @