Linux – Cat command and echo

catconcatenationecholinux

I'd like to concatenate the output from echo with content of a file. I've tried the following comand:

echo "abc" | cat 1.txt > 2.txt

but the 2.txt file only contains the content from 1.txt. Why doesn't it work?

Best Answer

It does not work because the cat program in your pipe sequence was not instructed to read the echo program's output from standard input.

You can use - as a pseudo file name to indicate standard input to cat. From man cat on an msys2 installation:

EXAMPLES
       cat f - g
              Output f's contents, then standard input, then g's contents.

So try

echo "abc" | cat - 1.txt > 2.txt

instead.

Related Question