I am following this bash shell scripting guide:
In the section Numeric Comparisons, it cites an example:
anny > num=`wc -l work.txt`
anny > echo $num
201
anny > if [ "$num" -gt "150" ]
More input> then echo ; echo "you've worked hard enough for today."
More input> echo ; fi
What seems to happen above is we store a string of commands in a bash variable and then we invoke echo on the variable. What seems to happen is the string is evaluated and the wc command is executed and returns the line count to the controlling terminal.
Ok, so I launch my terminal in Ubuntu 12.04 and try something similar:
$ touch sample.txt && echo "Hello World" > sample.txt
$ cat sample.txt
Hello World
$ num='wc -l sample.txt'
echo $num
wc -l sample.txt
Wait a second, that didn't evaluate the string and return the line count. That just echoed the string back to the terminal. Why did I get different results?
wc -l work.txt
, instead it's assigned the number 201.