bash command history

Yesterday morning after arriving at the office, I booted up my laptop as usual and tried to connect to my Linux development server using putty. However, the ssh connection couldn’t be established successfully. After struggling for a while, I found that Linux server crashed. That frustrated me a little. Finally I managed to setup a new Linux server in our openstack lab in a few minutes. After using the Linux server for half of a day, I found a problem: the command history in the shell were not persisted after I logged out the Linux OS. If I login to the Linux OS again, I couldn’t reference any command that were executed in the last ssh session. google is really a good search engine for technical points. I find the answer for this issue and post it here for further reference. The solution is as below:

edit ~/.bash_profile file and add the below environment variables:

export HISTSIZE=20000
export HISTFILE=~/.history
export HISTFILESIZE=20000

The first variable sets the number of commands to remember in the history. The second variable sets the name of the file, in which the command history will be stored. The third variable sets the maximum number of lines contained in the history file.

After adding the environment variables, you are required to logout and then login to the Linux OS again. After that action, these variables will take effect. To quickly reference any command that was executed before, type the key combination CTRL+R. Then input any key words. The first command in the history that matching the key words will show up in the bash prompt line. If this command is what you just want, hit the enter key to run it. If not, type the key combination CTRL+R again. Another command matching the key words in the history, if exists, will show up. This greatly reduces our effort and saves time.