rookie099
rookie099

Reputation: 2607

Pipe multiple verbatim lines into command in bash

I am trying this in GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu):

$ echo <<EOF | cat
> 1
> 2
> 3
> EOF

I would have expected three lines of output (with 1, 2, 3), but I receive an empty line. What am I missing (probably a simple mistake)? I am aware that this particular output can be produced in a simpler way; the example should serve as blueprint for a more substantial application.

Upvotes: 5

Views: 10767

Answers (2)

chepner
chepner

Reputation: 530960

echo doesn't read from standard input, but it doesn't need to. You can embed newlines in a string.

$ echo '1
> 2
> 3' | cat

You can also use printf to output each word on a separate line:

$ printf '%s\n' 1 2 3
1
2
3

Or use a command group to pipe the output of multiple commands as a whole, which is useful if you have something more complicated than a group of simple echo commands, but as an example:

{
  echo 1
  echo 2
  echo 3
} | cat

Upvotes: 7

Ole Tange
Ole Tange

Reputation: 33685

echo does not read from stdin. Maybe you are trying to do:

$ cat <<EOF | cat
> 1
> 2
> 3
> EOF

Which of course can be shortened to:

$ cat <<EOF
> 1
> 2
> 3
> EOF

Upvotes: 9

Related Questions