Two updates.
Unlike bash 'history', the 'fc' command does not go on the history until after the output is generated. So the correct command 'fc -l -1' instead of 'fc -l -2'.
Line 15 causes grief if you don't have a long history, perhaps because of tabs. I was getting empty aliases in ZSH with 10K commands in history. To correct it, I simplified line 15 by telling 'fc' to strip the history item number. Bash retains the leading whitespace but it doesn't matter.
So this line 15 works for me now in both shells with any length of history:
x=`fc -l -n -1`
Thanks for this tip! I've wanted to so something like this for a very long time!
in bash, "history 3" shows the last 3 history items. In zsh, history is just an internal equivalent to "fc -l", and "fc -l 2" shows history starting at item #2. To show the last 2 items, use "fc -l -2".
Fortunately in bash "fc -l -2" works exactly the same, so you can use that instead of "history 2" for both shells.