Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 ?
Please explain.

share|improve this question

migrated from stackoverflow.com Apr 18 '10 at 6:37

This question came from our site for professional and enthusiast programmers.

1  
Not to mention, I can't seem to repro this anyways... echo date | wc gives me 1-1-5 on a ubuntu system, which is the same result as wc and then typing date^D. –  Dav Apr 16 '10 at 11:24
    
Check again, its not :) –  Tim Post Apr 16 '10 at 11:25
1  
echo `date` | wc would produce the results you are talking about. Or better, date | wc. echo prints whatever comes after it, unless $() or `` tells the shell you want the output of another process. –  Tim Post Apr 16 '10 at 11:27

5 Answers 5

$ echo date | wc
      1       1       5

as bytes counter == 5 (= sizeof("date")) it seems that date wasn't run

but

$ echo date | xargs time

runs date as command

share|improve this answer
    
Ok..date wasn't running, instead it gives no. of characters of date. But still, it should show 1 1 4, as date has 4 characters. Why is it showing 5? –  Happy Mittal Apr 16 '10 at 11:33
    
Because there is a line feed character after the string "date" –  Aaron Digulla Apr 16 '10 at 11:37
    
But there is no linefeed character after date. It is simply date|wc. so only 'd', 'a', 't' and 'e' are passed to wc. –  Happy Mittal Apr 16 '10 at 11:47
2  
echo will add a line feed for you. Use -n to avoid the line feed. –  Dan Andreatta Apr 16 '10 at 11:50

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.

share|improve this answer

It doesn't.

Try:

echo date | cat > here.out

then try:

echo `date` | cat > here.out
share|improve this answer
3  
Congrats for a "Useless Use of Cat Award" :-) partmaps.org/era/unix/award.html –  Aaron Digulla Apr 16 '10 at 11:38
    
:)) Eh, guess I have to polish my answers. –  vladv Apr 16 '10 at 11:46
2  
@Aaron Digulla - he was trying to demonstrate this while using a pipe similar to what the OP described. So in this case, its not a useless cat, since it does make a sensible demonstration, given the question. +1. –  Tim Post Apr 16 '10 at 11:48

echo `date` is what you want.

share|improve this answer

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...

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.