title: Get name of last run program in Bash
body: <p>I have a bash script where I trap errors using the trap command, and I would like to be able to print the name of the last command (the one that failed)</p>
  
  <pre><code>#!/bin/bash
  function error
  {
     # echo program name
  }
  trap error ERR
   # Some commands ...
  /bin/false foo # For testing
  </code></pre>
  
  <p>I'm not sure what to put in the <code>error</code> function. I tried <code>echo $_</code> but that only works if the command has no arguments. I also tried with <code>!!</code> but that gives me "!!: command not found". At an interactive prompt (also bash) I get:</p>
  
  <pre><code>$ /bin/false foo
  $ !!
  /bin/false foo
  </code></pre>
  
  <p>which seems to be pretty much what I want. Why the difference?
  What is the easiest way to get the name of the previous command inside a script?</p>