I have three files ina a directory named One, Two, Three.
One contains:
asdfg
Two contains:
qwerty
Three contains:
zxcvb
When I give the following command:
$ls > One > Two > Three
Then I give the following command:
$cat One
Output:
Nothing
Then I give the following command:
$cat Two
Output:
Nothing
Then I give the following command:
$cat Three
Output:
One
Three
Two
Can anyone please shed light on what exaclty is happening here? Why do the files One & Two become empty and the why does the output of the ls command get written only to the file Three?
As mentioned here
bashdoesn’t support demultiplexing, and it doesn’t support multiplexing either. The effect that you’re seeing is that each file is openedO_TRUNC, which means all content is destroyed. When multiple redirections are specified they’re closed again right away, only the last redirection is kept and actually receives any data.Here’s an excerpt of
stracerunning the command:As you can see bash keeps overwriting the same file descriptor. so only the last one will receive any data. One work around is to use
teewhich writes what it receives onstdinto all its arguments andstdout:On a side note,
zshdoes support this: