Why does
echo "hello world" | cat
works while
cat < echo "hello world"
does not? My (incorrect) intuition is that pipe would redirect stdout
to cat
as stdin
.
catpipe
Why does
echo "hello world" | cat
works while
cat < echo "hello world"
does not? My (incorrect) intuition is that pipe would redirect stdout
to cat
as stdin
.
Best Answer
We have two similar but different things
A pipeline
The pipe control operators (
|
and|&
) connect the output of a command to the input of the following one in the pipeline. So the first example works, the output of theecho
command, "Hello word", is connected with the input of the followingcat
command, that assumes the standard input as input file if not else specified. Indeed we can read fromman cat
:and below the example with the simple invocation of
cat
A redirection, or better an attempt of input redirection
<
In this case
cat
command is taking its input from the standard input that you redirect with the operator<
from the file on the right side of<
... that is not a file. This is because it doesn't work.From the redirection section of the
man bash
In bash it works for different reasons
cat <(echo "hello world")
Process Substitutioncat <<< $(echo "hello world")
Here StringsReferences
man bash
and search for redirection, pipeline, Here Strings and Process substitutionman cat
just because we use it...