In bash, I often run awk programs with many preset variables. Most of the times I use the same variable names in both bash and awk, so I call awk like the following:
awk -v var1="$var1" -v var2="$var2" -v var3="$var3" ....... '
# awk code
'
This gets a little tedious when I have many variable names to type. Ideally, I would like to be able to run something like the following:
$(awkline var1 var2 var3 ......) '
# awk code
'
where the output of the command "awkline" is the correct format of the awk command. For example, the "awkline" program could look like:
#!/bin/bash
line="awk"
for i in $*; do line="$line -v $i="'"$'"$i"'"'; done
echo $line
If I then run "awkline a b c", I get what I expect:
awk -v a="$a" -v b="$b" -v c="$c"
However, if I try to run the command like $(awkline a b c), the value of the variable a inside awk becomes (literally including the quotes) "$a", and not the value of a, as I would like. How do I substitute all occurences of e.g. $a with the value of the variable a, so that I can run the awkline command?
eval
? From the bash man page:eval [arg ...] - The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval.