Cat command and echo
Solution 1:
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.
Solution 2:
As noted by others, your original command fails because cat 1.txt
disregards its standard input. Either indicate that it should be its first argument (cat - 1.txt
), or use block redirection to redirect echo abc
and cat 1.txt
together. To wit:
{ echo abc; cat 1.txt; } > 2.txt
Relevant excerpt from the manual (man bash
):
Compound Commands
A compound command is one of the following. In most cases a list in a command's description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.
(
list)
list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.
{
list;
}
list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters
(
and)
,{
and}
are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter.
The first option (subshell environment) has a bunch of side effects, most if not all of which are irrelevant to your scenario; however, if redirecting a bunch of commands' output is all you need, then Option #2 here (group command) is preferred.
Solution 3:
( echo "abc"; cat 1.txt ) > 2.txt
You piped the echo output to cat, but cat had no use for input, and ignored it. Now the commands run one after the other, and their output is grouped (the parentheses) and directed into 2.txt.
Solution 4:
In any POSIX shell you could use command substitution to use cat file
as input for echo
:
echo $(cat file1.txt) "This Too"
In Bash you could use process substitution and use echo as another "file" for cat
, as in:
cat file1.txt <(echo This Too)
and then pipe or redirect the output as you see fit.
Like every other answer says, cat ignores stdin if it's got a file to look at. (I do like Daniel & mvw's answers too, +1 for them)