Is there a way to get the last executed command possibly together with the arguments within a ksh script? I am using mksh with cygwin.

I tried fc -nl -1, but the script throws a "“fc: history functions not available” error.

I don't see an equivalent of the BASH_COMMAND variable in ksh.

Here is the excerpt from the script :

function trapper ()
  printf "culprit: "
  fc -nl -1

trap trapper ERR

grep -ic textdoesntexist test3

trapper executes once grep returns 1 , but I get a fc: history functions not available error.

As pointed out by Lee, hist might be useful , but it is available only with ksh93, which I am not allowed to use.

I tried by storing the command line string in a variable and use that variable within the trapper function.

function err_handler
        echo "ERR in ${cmd} trapped"
        return 0

trap err_handler EXIT ERR

cmd="grep -ic testdoesntexist test3"
${cmd} &>/dev/null

It works, but I kinda think it is ugly. Is there a better way out?

  • my ksh man page says fc is an alias for hist. Aliases may only be available in interactive shells, so try using hist instead of fc. Oct 4, 2013 at 10:43
  • @glennjackman thankyou. fc is an alias for hist only in ksh93, we use a version older than that.
    – user917279
    Oct 4, 2013 at 11:02
  • @glennjackman could you make that into an answer?
    – terdon
    Oct 4, 2013 at 15:37
  • "As pointed to by Lee in unix.stackexchange.com/a/27513/20291 , hist might be useful , but is available only with ksh93 , which I may not allowed to use." just to point this statement that was part of my original question.
    – user917279
    Oct 4, 2013 at 15:45

1 Answer 1


mksh author here ☻

The fc built-in utility accesses the interactive command line history. As you may have guessed from the name already, this is not available for scripts, since they are parsed differently (much more efficiently).

Generally speaking, in a script, you can always know the last line, anyway ;)

If you need access to an interactive shell’s history, define an alias or, preferably, a function in .profile or .mkshrc.

In your specific case, I suggest using explicit error handling, something like:

grep -ic textdoesntexist test3 || die 'Could not grep in test3'

And please really avoid using the GNU bash compatibility idiom of using &> as a redirection operator; it’s only there for compatibility, not new scripts, and goes away in -o posix mode already. Parsing it can break existing valid POSIX scripts.

  • "Generally speaking, in a script, you can always know the last line, anyway ;)" - I dont understand how can we know the last line, am I missing something big and fundamental here?
    – user917279
    Feb 28, 2014 at 17:08
  • If you have the script, you call a function, so you know where you come from. Admittedly not in your trap example, though…
    – mirabilos
    Feb 28, 2014 at 17:09
  • the statement within the function would know that the last statement was the function call itself. I am curious to know whether this understanding it right.
    – user917279
    Feb 28, 2014 at 17:25
  • Yes, exactly. But “fc” doesn’t operate on statements (which are handled as parse tree internally) but on input (editor) lines. This is just not available in a script.
    – mirabilos
    Feb 28, 2014 at 17:29
  • Fantastic, Stackexchange's Unix site was missing you for so long. Thank you for coming in to get us better.
    – user917279
    Feb 28, 2014 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .