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"
cmd=
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?
fcis an alias forhist. Aliases may only be available in interactive shells, so try usinghistinstead offc.