title: Reading a Bash variable from a multiline Here Document
body: <p>What is the <em>perfect</em> way to read a multiline variable from a Here Document in Bash?</p>
  
  <p>Look at how my way of doing it has the side-effect of returning <code>1</code> as <code>$?</code>...</p>
  
  <hr>
  
  <h3>Bash code:</h3>
  
  <pre><code>#!/bin/bash
  
  printf "Unsetting...\n"
  unset variable
  printf "Exit status: %s.\n" "$?"
  printf "Variable: %s.\n" "${variable}"
  
  printf "\n"
  
  printf "Setting to 'foo'...\n"
  variable='foo'
  printf "Exit status: %s.\n" "$?"
  printf "Variable: %s.\n" "${variable}"
  
  printf "\n"
  
  printf "Setting to 'bar' by reading stdin from process substitution...\n"
  read variable &lt; &lt;(echo 'bar')
  printf "Exit status: %s.\n" "$?"
  printf "Variable: %s.\n" "${variable}"
  
  printf "\n"
  
  printf "Setting to 'baz' by reading stdin from Here String...\n"
  here_string='baz'
  read variable &lt;&lt;&lt; "${here_string}"
  printf "Exit status: %s.\n" "$?"
  printf "Variable: %s.\n" "${variable}"
  
  printf "\n"
  
  printf "Setting to 'quux' by reading stdin from Here Document...\n"
  read variable &lt;&lt;- 'EOF'
  quux
  EOF
  printf "Exit status: %s.\n" "$?"
  printf "Variable: %s.\n" "${variable}"
  
  printf "\n"
  
  printf "Setting to 'thud &lt;newline&gt; thud' by reading stdin from Here Document...\n"
  read -d '' variable &lt;&lt;- 'EOF'
  thud
  thud
  EOF
  printf "Exit status: %s.\n" "$?" # ONE?!one!!oneone? :)
  printf "Variable: %s.\n" "${variable}"
  </code></pre>
  
  <hr>
  
  <h3>Output</h3>
  
  <pre><code>Unsetting...
  Exit status: 0.
  Variable: .
  
  Setting to 'foo'...
  Exit status: 0.
  Variable: foo.
  
  Setting to 'bar' by reading stdin from process substitution...
  Exit status: 0.
  Variable: bar.
  
  Setting to 'baz' by reading stdin from Here String...
  Exit status: 0.
  Variable: baz.
  
  Setting to 'quux' by reading stdin from Here Document...
  Exit status: 0.
  Variable: quux.
  
  Setting to 'thud &lt;newline&gt; thud' by reading stdin from Here
  Document...
  Exit status: 1.
  Variable: thud
  thud.
  </code></pre>