1 Kigazahn

Awk If Bash Variable Assignment

@slm has already included the POSIX docs - which are very helpful - but they don't really expand on how these parameters can be combined to affect one another. There is not yet any mention here of this form:

This is an excerpt from another answer of mine, and I think it demonstrates very well how these work:

Another example from same:

The above example takes advantage of all 4 forms of POSIX parameter substitution and their various or tests. There is more information in the link above, and here it is again.

Another thing that people often don't consider about is how very useful it can be in a here-document. Here's another excerpt from a different answer:


Here you'll set some defaults and prepare to print them when called...


This is where you define other functions to call on your print function based on their results...


You've got it all setup now, so here's where you'll execute and pull your results.


I'll go into why in a moment, but running the above produces the following results:

first run:

I went to your mother's house and saw Disney on Ice.

If you do calligraphy you will succeed.


I went to the cemetery and saw Disney on Ice.

If you do remedial mathematics you will succeed.


I went to the cemetery and saw Disney on Ice.

If you do remedial mathematics you will regret it.


The key feature here is the concept of You can set a variable to a value only if it is unset or null using the form:


If instead you wish to set only an unset variable, you would omit the and null values would remain as is.


You might notice that in the above example and get changed when set via even though has already been called, presumably setting them when it's run. The reason this works is that is a function - I enclosed it in rather than the used for the others. Because it is called in a subshell, every variable it sets is and as it returns to its parent shell those values disappear.

But when sets it is so it affects second evaluation of because sets only via

This article is part of the on-going Awk Tutorial and Examples series. Like any other programming languages, Awk also has user defined variables and built-in variables.

In this article let us review how to define and use awk variables.

  • Awk variables should begin with the letter, followed by it can consist of alpha numeric characters or underscore.
  • Keywords cannot be used as a awk variable
  • Awk does not support variable declaration like other programming languages
  • Its always better to initialize awk variables in BEGIN section, which will be executed only once in the beginning.
  • There are no datatypes in Awk. Whether a awk variable is to be treated as a number or as a string depends on the context it is used in.

Now let us review few simple examples to learn how to use user-defined awk variables.

Awk Example 1: Billing for Books

In this example, the input file bookdetails.txt contains records with fields — item number, Book name, Quantity and Rate per book.

$ cat bookdetails.txt 1 Linux-programming 2 450 2 Advanced-Linux 3 300 3 Computer-Networks 4 400 4 OOAD&UML 3 450 5 Java2 5 200

Now the following Awk script, reads and processes the above bookdetails.txt file, and generates report that displays — rate of each book sold, and total amount for all the books sold.

So far we have seen Awk reads the commands from the command line, but Awk can also read the commands from the file using -f option.

Syntax: $ awk -f script-filename inputfilename

Now our Awk script for billing calculation for books is given below.

$ cat book-calculation.awk BEGIN { total=0; } { itemno=$1; book=$2; bookamount=$3*$4; total=total+bookamount; print itemno," ", book,"\t","$"bookamount; } END { print "Total Amount = $"total; }

In the above script,

  • Awk BEGIN section initializes the variable total. itemno, total, book, bookamount are userdefined awk variables.
  • In the Awk Action section, Quantity*bookprice will be stored in a variable called bookamount. Each bookamount will be added with the total.
  • Finally in the Awk END section, total variable will have total amount.

Now execute the book-calculation.awk script to generate the report that displays each book rate and total amount as shown below.

$ awk -f book-calculation.awk bookdetails.txt 1 Linux-programming $900 2 Advanced-Linux $900 3 Computer-Networks $1600 4 OOAD&UML $1350 5 Java2 $1000 Total Amount = $5750

Awk Example 2. Student Mark Calculation

In this example, create an input file “student-marks.txt” with the following content — Student name, Roll Number, Test1 score, Test2 score and Test3 score.

$ cat student-marks.txt Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122 38 37 65 Edwin 2537 78 67 45 Dayan 2415 30 47 20

Now the following Awk script will calculate and generate the report to show the Average marks of each student, average of Test1, Test2 and Test3 scores.

$cat student.awk BEGIN { test1=0; test2=0; test3=0; print "Name\tRollNo\t Average Score"; } { total=$3+$4+$5; test1=test1+$3; test2=test2+$4; test3=test3+$5; print $1"\t"$2"\t",total/3; } END{ print "Average of Test1="test1/NR; print "Average of Test2="test2/NR; print "Average of Test3="test3/NR; }

In the above Awk script,

  • In the Awk BEGIN section all the awk variables are initialized to zero. test1, test2, test3 and total are user-defined awk variables.
  • In the Awk ACTION section, $3, $4, $5 are Test1, Test2 and Test3 scores respectively. total variable is the addition of 3 test scores for each student. The awk variable test1, test2 and test3 has the total scores of each corresponding test.
  • So in the Awk END section, dividing each test total by total number of records (i.e student) will give you the average score.  NR is an Awk built-in variable which gives total number of records in input.

Awk Example 3. HTML Report for Student Details

In the above two example, we have seen awk variable which has numbers as its values. This example shows awk script to generate the html report for the students name and their roll number.

$ cat string.awk BEGIN{ title="AWK"; print "<html>\n<title>"title"</title><body bgcolor=\"#ffffff\">\n<table border=1><th colspan=2 align=centre>Student Details</th>"; } { name=$1; rollno=$2; print "<tr><td>"name"</td><td>"rollno"</td></tr>"; } END { print "</table></body>\n</html>"; }

Use the same student-marks.txt input file that we created in the above example.

$ awk -f string.awk student-marks.txt <html> <title>AWK</title><body bgcolor="#ffffff"> <table border=1><th colspan=2 align=centre>Student Details</th> <tr><td>Jones</td><td>2143</td></tr> <tr><td>Gondrol</td><td>2321</td></tr> <tr><td>RinRao</td><td>2122</td></tr> <tr><td>Edwin</td><td>2537</td></tr> <tr><td>Dayan</td><td>2415</td></tr> </table></body> </html>

We can store the above output, which gives the following html table. In the above script, variable called name and rollno are string variable, because it is used in string context.

Student Details

Recommended Reading

Sed and Awk 101 Hacks, by Ramesh Natarajan. I spend several hours a day on UNIX / Linux environment dealing with text files (data, config, and log files). I use Sed and Awk for all my my text manipulation work. Based on my Sed and Awk experience, I’ve written Sed and Awk 101 Hacks eBook that contains 101 practical examples on various advanced features of Sed and Awk that will enhance your UNIX / Linux life. Even if you’ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Sed and Awk utilities.

If you enjoyed this article, you might also like..

Tagged as:Awk Tutorial Examples, Linux Awk Examples, Linux Awk Tutorial, Unix Awk Examples, Unix Awk Tutorial, Variables in Awk

Leave a Comment


Your email address will not be published. Required fields are marked *