To pass shell variables to an awk script in Bash, you can use the -v option of the awk command. The -v option allows you to pass shell variables to awk.
Here’s an example of using the -v option to pass a shell variable to an awk script:
# Define the shell variable
string="Hello World"
# Pass the shell variable to the awk script
echo "$string" | awk -v str="$string" '{print "The string is: " str}'
In this example, the shell variable string
is defined as “Hello World” and is passed to the awk script as the awk variable str
. The awk script then prints “The string is: Hello World”.
This is just a basic example, but you can use the -v option to pass multiple shell variables to an awk script and use them in more complex ways within the script.