I have file1, file2, file3.
file1 contains 1
file2 contains 2
file3 contains 3
I use command
cat file1 > file2 > file3
Results in:
file1 1
file2 (contains nothing)
file3 1
Why does anything along this line get destroyed? Basically what am I not seeing behind the scenes?
(Side notes using "append" >>
is even weirder)
Best Answer
Redirections in Bourne/POSIX-style shells such as bash, dash, ksh, etc.
> x
opens and truncates filex
, and sets the file descriptor that writes intox
as standard output. Your command:Will:
file2
file3
cat file1
The end result is that standard output points into
file3
at the timecat
runs. Bothfile2
andfile3
have their current contents erased, andfile3
gets the output ofcat
(the contents offile1
) written into it.If you want to split output into multiple streams written into separate files, you can use
tee
:Other shells (notably
zsh
) behave differently, and your command would have the result you probably expected: bothfile2
andfile3
would have the contents offile1
.Note that
cat
isn't necessary here;<
input redirection would do the job just as well.