By default, the history
command in Bash shows a list of recently executed commands with their respective line numbers. However, it does not display the date and time when each command was executed. (www.utahcnacenters.com) To show the date and time for each command in the Bash history, you can set the HISTTIMEFORMAT
environment variable.
Here’s how to do it:
- Open your Bash configuration file (
~/.bashrc
or~/.bash_profile
). - Add the following line to the file:
export HISTTIMEFORMAT="%F %T "
This sets the HISTTIMEFORMAT
environment variable to a string that specifies the date and time format. The format string %F %T
represents the date and time in the format YYYY-MM-DD HH:MM:SS
.
- Save the file and close it.
- Reload the Bash configuration by running the following command:
source ~/.bashrc
Now, when you run the history
command, it will display the date and time for each command in the Bash history, as shown below:
1 2022-02-15 14:07:21 ls
2 2022-02-15 14:07:25 cd ..
3 2022-02-15 14:07:29 ls
4 2022-02-15 14:07:34 history
This can be helpful in identifying when a particular command was executed and for troubleshooting purposes.