How Redirection Symbols Work in Bash Sequence

bashio-redirectionshell

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.

processed in the order they appear, from left to right

> x opens and truncates file x, and sets the file descriptor that writes into x as standard output. Your command:

cat file1 > file2 > file3

Will:

  1. Open and truncate file2
  2. Set standard output to write to that file descriptor
  3. Open and truncate file3
  4. Set standard output to write to that file descriptor
  5. Run cat file1

The end result is that standard output points into file3 at the time cat runs. Both file2 and file3 have their current contents erased, and file3 gets the output of cat (the contents of file1) written into it.


If you want to split output into multiple streams written into separate files, you can use tee:

cat file1 | tee file2 > file3

Other shells (notably zsh) behave differently, and your command would have the result you probably expected: both file2 and file3 would have the contents of file1.


Note that cat isn't necessary here; < input redirection would do the job just as well.

Related Question