bash, (g)awk, local variables - Quoting Rules

Nils Kassube kassube at gmx.net
Sat Jun 30 18:49:06 UTC 2012


franz.reitinger wrote:
> After reading the "quoting"-rules of awk/bash I tried some different
> possibilities to access bash variables inside awk.
> The following code also works within a bash (script): (v 4.2.24)
> 
> a=$(<anyFile)
> b="anyPattern"
> 
> awk 'BEGIN {print index("'"$a"'","'"$b"'")}'

With so many quotes that line looks quite error prone to me. Therefore I 
searched for "environment" in the gawk manual and found this solution:

export a b
awk 'BEGIN { print index(ENVIRON["a"], ENVIRON["b"]) }'


Nils




More information about the ubuntu-users mailing list