Format bash history output

You can modify the bash history output by specifying a custom format, e.g.

HISTTIMEFORMAT="%F %T "

where these placeholders stand for:

  • %F: full date (year-month-date)
  • %T: time (hour:minutes:seconds)

which gives us:

  115  2020-05-27 22:56:23 git mv /workspaces/megamorf.gitlab.io/_drafts/cheat-sheet-systemd5.md /workspaces/megamorf.gitlab.io/_drafts/cheat-sheet-systemd.md
  116  2020-05-27 22:56:23 git status
  117  2020-05-27 22:56:23 git diff _drafts/cheat-sheet-systemd.md
  118  2020-05-27 22:56:23 git add _drafts/cheat-sheet-systemd.md
  119  2020-05-27 22:56:23 git status
  120  2020-05-27 22:56:23 git commit -m "Update systemd draft"
  121  2020-05-27 22:56:23 git push
  122  2020-05-28 21:22:35 ls -al
  123  2020-05-28 21:22:44 history | tail -n 10
  124  2020-05-28 21:23:13 HISTTIMEFORMAT="%F %T " history | tail -n 10

Note:
The first time you enable timestamps for your bash history, you won’t see any date / time for your previously used commands. Only commands executed after you enable timestamps for your Bash history will show the correct date and time at the beginning of the line.

Examples for other time formats (see date man page for more format sequences):

  • "%Y/%m/%d %T " = “2020/05/28 21:28:49 …”
  • "%d/%m/%y %I:%M:%S %p " = “28/05/20 09:28:49 PM …”