Search code examples
linuxshellcatpiping

Shell script can not pass file data to shell input


cal April 2012 | cat > t | cat < t | more

Why does it showing nothing? Why isn't it showing

     April 2012       
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30  

Solution

  • | (anonymous pipe) connects stdout (1) of the first process with stdin (0) of the second. After redirecting the output to a file, there is no stdout left, so there's nothing to pipe. Also, cat | cat < file does not really make sense, it gets two inputs connected to stdin (at least with bash, redirection comes later and "wins": echo uiae | cat <somefile will output the content of somefile)

    If you want to display output of a command and, at the same time, write it to the file, use the tee binary. It writes to a file, but still writes to stdout

    cal April 2012 | tee t | more
    cat t # content of the above `cal` command