Simplest standard way to print last bash command with timestamp
As you point out, history 1
prints the history
command itself since that is the last command you ran. To get the previous one, you'd need history 2
:
$ touch foo
$ history 2
$ history 2
19950 touch foo
19951 history 2
So, to get the previous command without counting history
itself, pass it through head -n1
:
$ history 2 | head -n1
19952 touch foo
Then, add the time stamp in whatever format you want (see man 3 strftime
for available formats):
$ HISTTIMEFORMAT="%F %H:%m:%S " history 2 | head -n1
19959 2016-06-11 15:06:08 touch foo