when I write a command
$ echo date
then it prints "date" as it is i.e it doesn't run date program.
But when I write
$ echo date | wc
then correct answer is produced as if date was run. How piping is making difference here ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
It has to do with how bash uses strings.
If you look at what get's stored in those variables
d1=date d2="date" d3='date' d4=`date`
You will notice that d1,d2 and d3 just are strings that contains "date", d4 however has the result of the executed command date.
If we then take this a step further and see if we can find any difference between those strings.
d4=`date +%Y%m%d` echo $d4
That would mean that we now have "20100418" stored in $d4.
d3='$d4' echo $d3
Now in $d3 we have printed the string "$d4", those exact 3 characters...
d2="$d4" echo $d2
Now here we do have "20100418" stored in $d2 since we printed $d4 and saved that output into the variable $d2.
d1=$d4 echo $d1
And then you have a copy the content of variable $d4 into variable $d1.
Hope this clarifies a little how those strings work.
And now back to your question.
cj@zap:~$ echo date | wc 1 1 5 cj@zap:~$ echo `date` | wc 1 6 31 cj@zap:~$ date | wc 1 6 31
Now what does that actually mean? The man wc gives us this:
NAME wc - print newline, word, and byte counts for each file
So "1 1 5" just told us that we have 1 newline, 1 word and 5 characters, and that matches date\n.
And "1 6 31" will match "sön 18 apr 2010 10.07.25 CEST\n", since that was what my date command gave me...