title: What method should I use to write error messages to 'stderr' using 'printf' in a bash script?
body: <p>I want to direct the output of a <code>printf</code> in a <code>bash</code> script to <code>stderr</code> instead of <code>stdout</code>. </p>
  
  <p>I am <strong>not</strong> asking about redirecting either <code>stderr</code> or <code>stdout</code> from where ever they are currently routed. I just want to be able to send the output from a <code>printf</code> to <code>stderr</code> instead of to the default of <code>stdout</code>.</p>
  
  <p>I experimented a little and found that appending <code>1&gt;&amp;2</code> to the <code>printf</code>, as shown in the example below, appears to do what I want. However, I have <strong><em>no</em></strong> experience using bash. So my <em>primary</em> question is if there is a "<em>better</em>" way to do this in <code>bash</code>? </p>
  
  <p>By "<em>better</em>" I mean is there another way to do this which is more commonly used, more conventional, or more idiomatic? How would a more experienced bash programmer do it?</p>
  
  <pre><code>#!/bin/bash
  printf "{%s}   This should go to stderr.\n" "$(date)" 1&gt;&amp;2 
  printf "[(%s)] This should go to stdout.\n" "$(date)" 
  </code></pre>
  
  <p>I also have a <em>secondary</em> question. I am asking it not so much because I <em>need</em> to know, but more because I am just curious and would like to have a better understanding about what is happening.</p>
  
  <p>It seems the above will only work when it runs inside a shell script. It does not appear to work when I try it from a command line. </p>
  
  <p>Here is an example of what I mean. </p>
  
  <pre class="lang-none prettyprint-override"><code>irrational@VBx64:~$ printf "{%s} Sent to stderr.\n" "$(date)" 1&gt;&amp;2 2&gt; errors.txt
  {Sat Jun  9 14:08:46 EDT 2012} Sent to stderr.
  irrational@VBx64:~$ ls -l errors.txt
  -rw-rw-r-- 1 irrational irrational 0 Jun  9 14:39 errors.txt
  </code></pre>
  
  <p>I would expect the <code>printf</code> command above to have no output because the output should go to <code>stderr</code>, which in turn should go to a file. But this does not happen. Huh?</p>