Every system administrator is a frequent user of the bash history command to recall previously executed actions.
We frequently use the bash shells’ history feature to check what commands we executed previously on the system and also re-execute some of the commands if required. This often results in a lot of time being saved as we don’t have to retype lengthy commands on the terminal. We can just recall them from the shell history if we had used them earlier. We’ve already shared two posts earlier on the bash shell history namely bash history capabilities and replace occurrences of words in last command in bash history . In this article, we’ll show you how can use a few shell environment variables to make your bash shell history become very efficient.
Before we get to discussing the environment variables, we would like to mention how you could view only particular count of commands from the entire command line history. You could pipe the output of the history command to the head and/or tail commands if you are interested in only a subset of the complete output. Or you can use the history command followed by the number of commands you’d like to see. Note that this number will be in descending order from the most recently executed command. For example, to view the last 50 commands in history, type:
The above command will show you that last 50 commands that you had executed on the system. With this out of the way, we’ll now begin our discussion of some useful shell environment variables to customize bash history settings.
There might be circumstances when we don’t want to keep all the commands we type in history. For this we can use the HISTCONTROL variable. If we type HISTCONTROL=ignorespace then anything we type on the command prompt that begins with space instead of a character will not be recorded in history. If we want the shell history to ignore duplicate commands then we could see the value of HISTCONTROL variable to ignoredups.
echo "export HISTCONTROL=ignoredups:ignorespace" >> .bash_profile . .bash_profile
Note that this will not erase previous duplicates populated in the shell history. To erase them set the value to HISTCONTROL variable to erasedups.
To demonstrate let’s set HISTCONTROL=ignoredups:ignorespace and run the ls command thrice and then rn history command:
[ssuri@linuxnix:/tmp] $ ls check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-ssuri-kByu5Z [ssuri@linuxnix:/tmp] $ ls check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-ssuri-kByu5Z [ssuri@linuxnix:/tmp] $ ls check_roster_list.log hsperfdata_qscn nagiosxi vmware-root global-info.tmp lost+found service_account_password_check.log yum-ssuri-kByu5Z [ssuri@linuxnix:/tmp] $ history 10 642 vim .bashrc 643 uptime 644 cat .bash_profile 645 export HISTCONTROL=erasedups 646 HISTCONTROL=ignoredups:erasedups 647 shopt -s histappend 648 history 649 cd /tmp 650 ls 651 history 10
As you may notice from the above output, I executed the ls command thrice but it was recorded only once.
To demonstrate the ignorespace feature, I’ve run a couple of commands leaving a space at the beginning of the command prompt.
[ssuri@linuxnix:~] $ w 16:34:53 up 191 days, 22:38, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ssuri pts/0 linuxnix. 16:17 0.00s 0.01s 0.00s w [ssuri@linuxnix:~] $ uptime 16:34:56 up 191 days, 22:39, 1 user, load average: 0.00, 0.00, 0.00 [ssuri@linuxnix:~] $ who ssuri pts/0 2017-12-29 16:17 (linuxnix.example.org) [ssuri@linuxnix:~] $ history 10 645 w 646 uptime 647 whoami 648 history 10 649 . .bahrc 650 . bashrc 651 cd 652 . .bashrc 653 who 654 history 10 [ssuri@linuxnix:~] $
Notice that the commands w and uptime had space in the beginning and did not get recorded in history.
There might be some commands like ls or history that you’d never want to get recorded. For this purpose, we use the HISTIGNORE variable. For example, to ignore the history command from being recorded, type the following:
Now, let us try this out.
[ssuri@linuxnix:~] $ history 2 666 df -h 667 ls [ssuri@linuxnix:~] $ history 2 666 df -h 667 ls [ssuri@linuxnix:~] $
Notice that I typed history 2 and then history 2 again but the history command execution was not recorded.
The HISTTIMEFORMAT variable allows us to add a time stamp to our history commands. Let’s demonstrate this on the command line.
[ssuri@linuxnix:~] $ HISTTIMEFORMAT="%h %d %H:%M:%S> " [ssuri@linuxnix:~] $ uptime 16:46:22 up 191 days, 22:50, 1 user, load average: 0.03, 0.02, 0.00 [ssuri@linuxnix:~] $ w 16:46:23 up 191 days, 22:50, 1 user, load average: 0.03, 0.02, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ssuri pts/0 linuxnix. 16:17 0.00s 0.04s 0.00s w [ssuri@linuxnix:~] $ history 2 672 Dec 29 16:46:22> uptime 673 Dec 29 16:46:23> w [ssuri@linuxnix:~] $
To get a full list of available variables you may please check the man page for strftime.
By default, the shell history is saved up to 1000 commands depending on the operating system. We can change this value by using the HISTSIZE variable to a modify the number of commands to be saved. Set its value to -1 for no limit or to 0 to stop recording commands.
When we exit the shell our command line history is saved onto the file .bash_history. The size of this file is 1000 lines by default. To modify it, we need to change the value of HISTFILESIZE variable and set it in the .bash_profile file.
In this article, we discussed some environment variables whose values we may set and tune appropriately to use enhance our usage of the bash history feature and improve our productivity on the command line.
Please do note that you need to add and export the values of these environment variables in the .bash_profile file in your home directory in order for the values of the variables to take effect in every login session.
Latest posts by Sahil Suri (see all)
- How does /tmp directory get emptied automatically over time ? - January 22, 2019
- Oracle VirtualBox installation troubleshooting in Linux explained - October 23, 2018
- How to install Oracle virtual box on Centos 7? - October 22, 2018
- Understanding swapiness parameter in Linux - September 20, 2018
- Resolving YUM: Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast - September 19, 2018