You are doing nothing wrong: this is what we should expect.
The first cat <&0
consumes the entire contents of standard input because that's what cat
does: it reads all of its input until the end.
When the second cat <&0
runs, there is nothing left to consume on standard input: the end of file was already reached previously.
If, in a shell script, you need to make 2 or more passes through your standard input, you have to dump it into a temp file, then process the temporary file as many times as you want.
Securely creating temporary files in /tmp
and making sure they are correctly disposed of when your script terminates or dies is left as an exercise for you :-)
By the way, the <&0
is unnecessary and does nothing. Its function is to point standard input to file descriptor 0... which is standard input... which is by definition where standard input already points! You can just make that command cat
alone instead.
cat <&0
in the first place?cat <&0
instead of justcat
?<&0
only duplicates (redundantly and idempotently) the file descriptor, not the data available on that descriptor.