title: How to printf an exclamation mark in bash?
body: <p>I need to printf a simple script and redirect the output to a file, but when I do this:</p>
  
  <pre><code>printf "#!/bin/bash\ntouch /tmp/1234567890_$RUN" &gt; /tmp/password-change-script_$RUN.sh
  </code></pre>
  
  <p>I get this error:</p>
  
  <p>bash: !/bin/bash\ntouch: event not found</p>
  
  <p>If I escape the exclamation mark:</p>
  
  <pre><code>printf "#\!/bin/bash\ntouch /tmp/1234567890_$RUN" &gt; /tmp/password-change-script_$RUN.sh
  </code></pre>
  
  <p>Then the escape character is still present in the file.</p>
  
  <pre><code>cat /tmp/password-change-script_$RUN.sh
  #\!/bin/bash
  touch /tmp/1234567890_111
  </code></pre>
  
  <p>By the way, in this particular case, the #!/bin/bash MUST be in the file. For some reason the binary file that executes the script won't read the file otherwise.</p>