title: Read file into variable while suppressing "No such file or directory" error
body: <p>I want to read text file contents into a Bash variable, suppressing the error message if that file does not exist. In POSIX, I would do</p>
  
  <pre><code>var=$(cat file 2&gt;/dev/null)
  </code></pre>
  
  <p>But I read (e.g. at <a href="https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell">How to read a file into a variable in shell</a>) that it's a <em>Useless Use of Cat</em> in Bash. So, I'm trying those:</p>
  
  <pre><code>var=$(&lt; file 2&gt;/dev/null)
  var=$(&lt; file) 2&gt;/dev/null
  </code></pre>
  
  <p>But it the first doesn't read an existing file, and both print <code>-bash: file: No such file or directory</code> if the file does not exist. <strong>Why doesn't this work?</strong> (Especially: What completely breaks the first one?)</p>
  
  <p>What does work is this:</p>
  
  <pre><code>{ var=$(&lt; file); } 2&gt;/dev/null
  </code></pre>
  
  <p>But it's ugly and cumbersome. So, <strong>is there a nicer syntax, or is this a valid use of cat after all?</strong></p>