Assigning output from awk to variable

user1929905 picture user1929905 · Dec 27, 2012 · Viewed 16.4k times · Source

I have a script whose contents are as below:

    result= awk 's=100 END  {print  s }' 
    echo "The result is" $result

The desired output is:

The result is 100

My script is running without exiting and I am also not getting the desired output. Please help.

Answer

dogbane picture dogbane · Dec 27, 2012

Use command substitution to assign the output of a command to a variable.

The syntax is: var1=$(command).

result=$(awk 'BEGIN{s=100} END {print s}' /dev/null)
echo "The result is $result"