BASH - Bourne Again SHell
Bash is still my Unix shell of choice in 2020, and history is its major functionality that I rely on. This post will be an experiment: I want to collect as much useful information as I can remember in a single post about a particular topic. It’s a concept very similar to Unix Reference pages, but for smaller topics.
Shell history is a functionality that allows you to use history command for quickly accessing the commands you typed in the past:
Bash history is stored in the user’s home directory, in the /home/$USER/.bash_history file – so if your username is greys (like mine!), the location for Bash history is /home/greys/.bash_history.
There’s a special command called history – you need to learn it. This isn’t a separate binary but usually a built-in function in shells like Bash.
You very learn typing “history” – this returns all of the commands you’ve typed before, then start using history command with grep to find most relevant results, like this:
… and then progress to REALLY make the most of bash history by using its built-in history management features, like reverse search:
Press Ctrl+r right there in the prompt and bash will want you to start typing pattern for searching in its history. It shows you the best matched line from history and if you press Enter you’ll execute that command from history again:
Next evolutionary step is to equip your shell history with proper timestamps – instead of just knowing the order in which your past commands ran, you’ll know exactly when this happened:
This will give you history like this going forward:
IMPORTANT: this will only add meaningful timestamps to current and future commands. All the ones in the past will have no valid timestamp, so they’ll just show you the date when you enabled history using HISTTIMEFORMAT.
You need to save history settings in your bash profile – this makes sure the same settings are applied next time you SSH login to the server or start another terminal window with Bash prompt on your desktop.
Check for anything like HIST (not HISTORY because some parameters are shortened) in the /home/$USER/.bash_profile file of yours:
See? If I want to update the HISTTIMEFORMAT, I don’t need to add new option to .bash_profile – I’ll just find and edit the existing one.
Let’s say I want to update HISTFILESIZE parameter to specify maximum size of the .bash_history file. Here’s how I do it (assuming it’s not found in Step 1):
#Temporarily Disable Bash History If you need to run a few sensitive commands like API keys or passwords that are passed as command line options, it’s probably best NOT to commit them into bash history.
Just unset the HISTFILE right from command line before you type anything sensitive:
After that your history won’t be committed even when you finish your current Bash session.
IMPORTANT: best do this in brand new Bash session – otherwise you may lost relevant history that happened prior to you unsetting HISTFILE.
Once timestamps are showing and history is tracking, you will discover a peculiar thing: some servers you’ll be accessing via multiple remote sessions, and their history won’t all necessarily be captured.
To help with this, we need to centralize bash history – that is, configure your bash profile to commit history more often (via PROMPT_COMMAND variable):
What this will do is: