perform echo and awk inside a string

hi,

just wanted to make a shortcut of this one
a="a b c"
b=`echo $a | awk '{print $2}'`
echo "the middle is $b"

why can't i do this:
a="a b c"
echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution :wall:

thanks

echo "the middle is `echo $a | awk '{print $2}'`"
1 Like

It should be:

 
a="a b c"
echo "the middle is $(echo $a | awk '{print $2}')" 

[PS: U should keep your Codes/Output in CODE Tag. Mods don't like unformatted codes and output. SO next time please use Code Tags]

Either way is fine. Backticks are used in post #2.. $() will work equally well.

Oopps... We both posted nearly at the same time so did not see ur reply :stuck_out_tongue:
I like $() more then `` because of aeshetic value and less confusion over `` and '' :slight_smile:

Plus in the backquote style command substitution you sometimes need to escape certain things (for example when nesting) whereas in $() this is much more straightforward..

Plus, backticks is obsolete, $( ) is recommended.

--ahamed