Take control of your bash_history

 

I spend most of my time working in front of a black and white terminal of remote SSH connections to various servers.

This means that I use bash (as my preferred shell) most of the day. And bash history is a very important feature of bash that saves me much time by recalling previous commands I have typed.

Here are some tricks on how you can optimize with some simple configurations settings the usage your bash history.

1. Append to History

history -a

Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

2. Clear History

history -c

(clear the history)

3. Write History to file

history -w 

(write to the file – overwrite!)

4. Dont save duplicate commands in the history

HISTCONTROL=ignoreboth

5. Size of the history:

HISTSIZE=500

HISTSIZE: The number of commands to remember in the command history. The default value is 500.

How do you set these 4-5 options? Either export them in your environment in your personal bash configuration file (~/.bashrc) or in the global bash configuration file (/etc/bash.bashrc).

export HISTCONTROL=ignoreboth
export HISTSIZE=500