Append text to a piped file Append text to a piped file unix unix

Append text to a piped file


This should do it:

(cat filename.txt && echo "text to append") | final_command

If you don't want a newline character at the end, use echo -n:

(cat filename.txt && echo -n "text to append") | final_command


A couple more alternatives:

cat filename.txt <(echo "text to append") | final_commandfinal_command <(cat filename.txt; echo "text to append")

(assuming final_command can take input from an argument instead of the default stdin)


In my opinion, this solution is more concise:

echo "text to append" | cat filename.txt - | final_command

It is also more flexible, for instance, you can

echo "text to append" | cat file1.txt - file2.txt ... | final_command

The solution which OP chose would be awkward in the second case.