Bash variable from command with pipes, quotes, etc

bashpipevariables

I have a command I'm using to get the hostname.localdomain:

dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'

This nicely returns a result like:

michael.lawler.localdomain.com

I'd like to further use that result as a variable in a Bash script.

It seems I'm having trouble getting past the first pipe.

If I VAR="dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'"

…I get back the entire zone transfer. I've also tried many minor changes, adding $ before the dig command, without quotes, but nothing seems to work. How can I fix this?

Best Answer

VAR=$( dig axfr @dc1.localdomain.com localdomain.com |
     grep -i Lawler |
     awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }' )