title: Bash read/write file descriptors -- seek to start of file
body: <p>I tried to use the read/write file descriptor in bash so that I could delete the file that the file descriptor referred to afterward, as such:</p>
  
  <pre><code>F=$(mktemp)
  exec 3&lt;&gt; "$F"
  rm -f "$F"
  
  echo "Hello world" &gt;&amp;3
  cat &lt;&amp;3
  </code></pre>
  
  <p>but the <code>cat</code> command gives no output. I can achieve what I want if I use separate file descriptors for reading and writing:</p>
  
  <pre><code>F=$(mktemp)
  exec 3&gt; "$F"
  exec 4&lt; "$F"
  rm -f "$F"
  
  echo "Hello world" &gt;&amp;3
  cat &lt;&amp;4
  </code></pre>
  
  <p>which prints <code>Hello world</code>.</p>
  
  <p>I suspected that bash doesn't automatically seek to the start of the file descriptor when you switch from writing to reading it, and the following combination of bash and python code confirms this:</p>
  
  <p>fdrw.sh</p>
  
  <pre><code>exec 3&lt;&gt; tmp
  rm tmp
  
  echo "Hello world" &gt;&amp;3
  exec python fdrw.py
  </code></pre>
  
  <p>fdrw.py</p>
  
  <pre><code>import os  
  
  f = os.fdopen(3)
  print f.tell()
  print f.read()
  </code></pre>
  
  <p>which gives:</p>
  
  <pre><code>$ bash fdrw.sh
  12
  
  $ # This is the prompt reappearing
  </code></pre>
  
  <p>Is there a way to achieve what I want just using bash?</p>