0

I'm trying to create a function that will echo the previous to last command from the current terminal. This is the expected output (here I call the desired function f):

$ history 1
history 1
$ f
history 1

Side question: Where does history(1) saves the current terminal's history. I know that when the terminal session ends history is typically saved in $HISTFILE (which is set to ~/.bash_history in my case). But I'd like to know where history(1) keeps the history of the currently running terminal.

1 Answers1

1

Found a solution:

f()(echo $(history 2 | head -n1 | cut -d' ' -f2-))

or using fc as suggested by G-Man:

f()(fc -ln -1)

test:

$ echo 123*
123*
$ f
echo 123*