Why does echo print a blank line when I pipe a file into it?

May 30, 2025

Suppose note.txt contains one line that says Hello. What will these commands print?

cat note.txt | echo         # a blank line
cat note.txt | echo Ready   # Ready

The pipe (|) connects the output of cat to standard input (stdin), a stream the next command can read. Whether it reads that stream depends on the command. As Stéphane Chazelas puts it, “echo doesn't read its standard input.”1

echo prints its arguments, the words the shell passes to it on the command line, followed by a newline. In the first example, echo receives zero arguments, so all you see is the newline. In the second, it receives one argument, Ready.

The same rule explains echo < note.txt. The shell opens the file as standard input. The command still has zero arguments. Try changing just the command name to cat < note.txt. With no filenames of its own, cat reads standard input and prints Hello.

If you want to append that line to another file, let cat read it and use >> to send the output to the end of the destination file.2

cat note.txt >> journal.txt

Footnotes

  1. Chazelas, S. (2013, February 4). Answer to “Redirecting the content of a file to the command ‘echo’”, para. 1. Unix & Linux Stack Exchange. ↩

  2. Ankur. (2012, June 2). Answer to “Shell: read a file and echo it's contents to another file”. Stack Overflow. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-05-30). Why does echo print a blank line when I pipe a file into it?. https://caiborui.com/blog/2025-05-30-en-why-echo-ignores-stdin

@misc{borui2025-eb24cd6d,
  author = {borui},
  title = {{Why does echo print a blank line when I pipe a file into it?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-05-30-en-why-echo-ignores-stdin}
}