title: Access to last command in Zsh (not previous command line)
body: <p>Is there a way to retrieve the text of the last command in Zsh? I mean the last executed command, not the last command line.</p>
  
  <h2>What was tried</h2>
  
  <p>Trying to do it, I discovered a subtle difference between Zsh and Bash handling of history. This difference is exposed in the following sample, which is the base of what I was doing in Bash, and which does not work in Zsh.</p>
  
  <pre><code>$ zsh
  $ echo "This is command #1"
  &gt; This is command #1
  $ echo "This is command #2"; echo $(history | tail -n1)
  &gt; This is command #2
  &gt; 3807 echo "This is command #1"
  
  $ bash
  $ echo "This is command #1"
  &gt; This is command #1
  $ echo "This is command #2"; echo $(history | tail -n1)
  &gt; This is command #2
  &gt; 4970 echo "This is command #2"; echo $(history | tail -n1)
  </code></pre>
  
  <p>Same tests, whose result differs in the last line. Bash appends the command line to the history, <em>before execution</em> (I don't know if it's by specification), while Zsh seems to append the command line to the history, <em>after execution</em> (the same, I don't know if it's by specification), thus <code>history | tail -n1</code> does not give the same.</p>
  
  <p>What I want, is to be able to retrieve <code>echo "This is command #2"</code>, that is, the text of the previous command even when it's on the same command line as other commands (when there are multiple command separated with <code>;</code>).</p>
  
  <p>With bash, I could use <code>history | tail -n1 | sed ...</code>, but this does not work any more with Zsh, due the difference in history handling.</p>
  
  <h2>What needs to be achieved</h2>
  
  <p>Any idea on how to get the last command from a multi‑command command line in Zsh?</p>
  
  <p>I need it for when a command needs to know what was the previous command, whatever that command was on the same line or the previous line. Another way to say it, could be: I need to access the last item of a single command oriented history, not of a command(s) line oriented history.</p>