awk - passing variables in and out

Am looking to pass some Linux environment variables into AWK , can I simply use the -v option ?

awk -F: -v AHOME=$HOME '{ if [ $1 == "X" ] 
                                        {rm AHOME/file.txt
                                         a=2 }  }'
                                         config.txt 

how do I pass variable a FROM Awk (defined and value set in Awk) back to the parent shell script ?

Please use code tags as required by forum rules!

You can't. awk prints to stdout that can be redirected, or to files defined within. You can use "command substitution" to assign a command's stdout to a variable, or you can pipe awk 's stdout into a read command to read the variables.

thanks, added the correct code tags.

So looks like using print > file within Awk and then grabbing the value from the file from the parent shell is one of my options.

You don't pass variables out, because awk is not a shell builtin and can't modify the shell's variables for you.

If you have multiple values to extract, file would be best. If it's just one, VAR=$(awk '{ stuff }' inputfile )

Pass in variables: awk -v var1="$var1" -v var2="$var2" ...
For safety in the shell each $variable in command arguments should be quoted!
Pass out variables works with eval:

eval `
echo 2 3 | awk '{ result=$1+$2; printf "result='\'%s\''\n", result }'
`
echo "$result"

For safety the variable's value is put in 'ticks'.
NB this method works with more than one variables.
For one variable the following is much simpler:

result=`
echo 2 3 | awk '{ result=$1+$2; print result }'
`
echo "$result"

NB the old-style backticks can be replaced by $( ) e.g.

result=$(
 echo 2 3 | awk '{ result=$1+$2; print result }'
)