5.4 Evaluation of commands on the fly

Capturing STDOUT as string

BASH offers an easy way to evaluate a specific command and then using the resulting STDOUT for further evaluation: simply put backticks ` or $() around the command.

Evaluate commands on the fly

for i in seq 1 3; do echo $i; done
#seq
#1
#3
for i in `seq 1 3`; do echo $i; done
#1
#2
#3
for i in $(seq 1 3); do echo $i; done
#1
#2
#3
echo "I like `echo "2+5" | bc`!"
#I like 7!
if [ 1 -gt $(echo "3 - 4" | bc) ]; then echo "true!"; else echo "false!"; fi
#true!

If we don’t provide the backstick or $(), the for loop will take the given command as a list of strings instead of evaluating it.