Oct 03

Bash History Lesson #1 - Timestamp

Mon, 10/03/2016 - 08:18 — peter

The Problem

The command line is the developer's number 1 friend and ally, but usually out-of-the-box configuration is very bare and uninformative. Every seasoned developer out there will have their own way of customizing the shell prompt, shell commands and even which shell better fits their needs. While this post will focus on the bash history command, bash being the default shell on the vast majority of systems out there, the same concepts can be used to customize other shells as well.

To be clear: we are going to spice up the results of the history command on the bash shell.

The Solution

There are a couple of files you can edit to customize your shell settings, the best choice depends on which operating system you are using. Personally, I add a new file to /usr/local/bin/ and put my settings there, then sourcing that file from another file like bashrc or profile. Feel free to put the suggested commands below on the file of your choosing.

History Timestamp

One of the most important changes I've made to my bash settings is adding a timestamp. This will tell you not only what command was typed, but also when the command happened, which gives you better context when you are sifting through bash history trying to figure out what you did last time you fixed the same thing you are trying to do now... To do that, just add the following to bashrc or your favorite configuration file:

HISTTIMEFORMAT="%Y-%m-%d %T "

What that does is add the date and time stamp prefix to each line on your bash history.

Easy Peasy!

-PCP