title: Does bash really truncate output redirection files at opening?
body: <p>From what I've read, bash is supposed to truncate files when it opens them. However after I run the command <code>echo hello &gt; file1 | ls &gt; file1 &gt; file2</code>, sometimes file1 contains &quot;hello&quot;, sometimes it doesn't contain anything. It is a really weird behavior, and I can't even find out what is the pattern, it seems aleatory. Here is for example the result of a few tries in my bash, sometimes deleting the first output file before, sometimes not:</p>
  <pre><code>bash-3.2$ echo hello &gt; output2 | ls &gt; output2 &gt; output3
  bash-3.2$ cat output2
  hello
  bash-3.2$ rm output2
  bash-3.2$ echo hello &gt; output2 | ls &gt; output2 &gt; output3
  bash-3.2$ cat output2
  bash-3.2$ echo hello &gt; output2 | ls &gt; output2 &gt; output3
  bash-3.2$ cat output2
  hello
  bash-3.2$ rm output2
  bash-3.2$ echo hello &gt; output2 | ls &gt; output2 &gt; output3
  bash-3.2$ cat ouput2
  cat: ouput2: No such file or directory
  bash-3.2$ cat output2
  bash-3.2$ echo hello &gt; output2 | ls &gt; output2 &gt; output3
  bash-3.2$ cat output2
  hello
  </code></pre>
  <p>I need to reproduce bash behavior in C so I really need to see what is the pattern here, why files are sometimes truncated and sometimes not</p>
  <p>EDIT: I know this command makes no sense in itself, I just need to reproduce bash's behavior in C with any command, even the most stupid one, so I need to know how it works here. I just typed a command easy to understand with no context to illustrate my problem</p>