I am aware that it is possible to add timestamps to bash_history.
The examples I've seen so far it always uses the current system time. And here is the problem for me: On the one hand, I like to keep logs in UTC because it makes it way easier to correlate stuff. On the other hand I'm working in a non-UTC timezone so my system time is not set to UTC.
Is there a way to have your system set to e.g., UTC+1 but still log bash_history using the UTC time?
Okay so based on the answer by @meuh I found a way to achieve what I was looking for:
- Set HISTTIMEFORMAT as you like. It only controls formatting (duh!). I chose:
HISTTIMEFORMAT='%F %T %z '
- To display the history in UTC on a system in another timezone, set the TZ variable to UTC for that command only:
$TZ='UTC' history
....
1040 2023-05-27 12:30:02 +0000 cat ~/.bashrc
1041 2023-05-27 12:30:11 +0000 grep -i hist ~/.bashrc
1042 2023-05-27 12:36:21 +0000 TZ='UTC' history
Run history without setting TZ and you get the time presented in your current TZ (compare the numeric timezone in the output to previous one):
$ history
...
1040 2023-05-27 14:30:02 +0200 cat ~/.bashrc
1041 2023-05-27 14:30:11 +0200 grep -i hist ~/.bashrc
1042 2023-05-27 14:36:21 +0200 TZ='UTC' history