≡ Menu

Bash: Pass Shell Variables To awk

How do I pass shell variables to awk command or script under UNIX like operating systems?

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 root value - " r}'

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:
$ ./script.sh "vivek" /etc/passwd
Sample outputs:

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

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
Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 8 comments… add one }
  • David May 18, 2012, 8:22 pm

    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 January 10, 2013, 3:28 pm

      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 January 17, 2013, 8:06 am

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

  • David May 18, 2012, 8:24 pm

    Works well once you have the format

  • prats September 25, 2013, 10:31 am

    Thanks -v works :)

  • volkan July 15, 2015, 9:17 am

    Thank you.

  • simgirl November 5, 2015, 5:55 pm

    Bless you

  • l10nn April 2, 2016, 12:12 am

    Great information!

    Thanks a lot

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , ,