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 ?
migrated from stackoverflow.com Apr 18 '10 at 6:37
This question came from our site for professional and enthusiast programmers.
It has to do with how bash uses strings.
If you look at what get's stored in those variables
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.
That would mean that we now have "20100418" stored in $d4.
Now in $d3 we have printed the string "$d4", those exact 3 characters...
Now here we do have "20100418" stored in $d2 since we printed $d4 and saved that output into the variable $d2.
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.
Now what does that actually mean? The man wc gives us this:
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...
$ echo date | wc 1 1 5
as bytes counter == 5 (= sizeof("date")) it seems that date wasn't run
$ echo date | xargs time
runs date as command
The problem is 'echo' sends the following text to the standard output. The text, in this case, is 'date' so what you show won't work. This will: echo|date because date now produces the text for echo.