Search code examples
bashpipeechocatheredoc

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.


Solution

  • 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