Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Note that num is never assigned the string wc -l work.txt, instead it's assigned the number 201. –  ignis Mar 17 at 9:13

3 Answers 3

up vote 1 down vote accepted

You need to use backticks to evaluate the expression.

$ num=`wc -l sample.txt`
$ echo $num
1 sample.txt

If you want to see only "1" in the output, use the command

$ num=`cat sample.txt | wc -l`
$ echo $num
1

And also works:

$ num=`wc -l < sample.txt`
$ echo $num
1

For additional information, see Differences between doublequotes " ", singlequotes ' ' and backticks ´ ´ on commandline?

share|improve this answer
    
Any idea why I get '1 sample.txt' and not just the number '1'? –  JohnMerlino Mar 17 at 5:20
    
That's the way how wc command works. Try cat sample.txt | wc -l. –  Danatela Mar 17 at 5:22
    
wc -l displays the number of lines present in a file along with it's filename. –  Avinash Raj Mar 17 at 5:25

Please note that symbol:

'

Single quote

   Enclosing characters in single quotes preserves the  literal  value  of
   each character within the quotes.  A single quote may not occur between
   single quotes, even when preceded by a backslash.

and

`

Backquote

   Command substitution allows the output of a command to replace the com‐
   mand name.  There are two forms:

          $(command)
   or
          `command`

   Bash performs the expansion by executing command and replacing the com‐
   mand  substitution  with  the  standard output of the command, with any
   trailing newlines deleted.

So the Backquote is returning result of the command to Standard Output. Thats why

`wc -l sample.txt`

returns results of the command, while

'wc -l sample.txt'

just return string "wc -l sample.txt"

share|improve this answer

If you want to capture the output of a command in a variable, you need to either use backticks `` or enclose the command in $():

$ d=$(date)
$ echo "$d"
Mon Mar 17 10:22:25 CET 2014
$ d=`date`
$ echo "$d"
Mon Mar 17 10:22:25 CET 2014

Note that the string is actually evaluated at the moment of the variable declaration, not when you echo it. The command is actually run within the $() or backticks and the output of that command is saved as the variable's value.

In general, you should always use $() instead of backticks which are deprecated and only around for compatibility reasons and much more limited. You cannot, for example, nest commands within backticks but you can with $():

$ echo $(date -d $(echo yesterday))
Sun Mar 16 10:26:07 CET 2014

See this thread on U&L for some more details of why `` should be avoided.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.