Passing variables from the outside
So far, we only used variables inside the awk script. But what if we wanted to pass a Bash variable into an awk script? This can be useful if you want to write a general script that you can call with different parameters, or because your script depends on system parameters.
For example, let’s define a variable that represents the tax rate:
We can now use -v to pass this variable into awk:
To add the tax onto the price column (5th column), we first need to remove the dollar sign from the price so we can do arithmetic on it. Let’s use substr to fetch everything after the first character:
Now combine that with the variable to calculate the price including tax: