title: getting last executed command from script
body: <p>I'm trying to get last executed command from command line from a script to be saved for a later reference:</p>
  
  <p>Example: </p>
  
  <pre><code># echo "Hello World!!!"
  &gt; Hello World!!!
  # my_script.sh
  &gt; echo "Hello World!!!"
  </code></pre>
  
  <p>and the content of the script would be :</p>
  
  <pre><code>#!/usr/bin/ksh
  
  fc -nl -1 | sed -n 1p
  </code></pre>
  
  <p>Now as you notices using here <code>ksh</code> and <code>fc</code> is a built in command which if understood correctly should be implemented by any POSIX compatible shells. [I understand that this feature is interactive and that calling same <code>fc</code> again will give different result but this is not the concern do discuss about]</p>
  
  <p>Above works so far so good only if <code>my_script.sh</code> is being called from the shell which is as well <code>ksh</code>, or if calling from <code>bash</code> and changing 1st line of script as <code>#!/bin/bash</code> then it works too and it doesn't if shells are different.</p>
  
  <p>I would like to know if there is any good way to achieve above without being constrained by the shell your script is called from. I understand that <code>fc</code> is a built in command and it works per shell thus most probably my approach is not good at all from what I want to achieve. Any better suggestions? </p>