The bash shell maintains a history of the commands you entered. You can re-execute a command by recalling it from the history, without having to re-type it and everyone knows this. So lets go further ...
The command history is stored in a file, specified by an environment variable.
$ echo $HISTFILE
/home/shanks/.bash_history or
/root/.bash_history (if you work as root)
The maximum number of commands that it will save depends on the value of this environment variable:
$ echo $HISTSIZE
You may set the HISTSIZE variable in
# cat /etc/profile | grep HISTSIZE=
HISTSIZE=1000
Life is simple if we operate on a single shell session history at any given time.
If you have 2 simultaneous sessions, the history in session2 does not have the commands you expect from session1.
This at times can become quite irritating since we would like to have history commands synced across sessions. Also you may want to know what command is being executed if you happen to give someone access to your system.
The remedy is simple. Change the history behavior as follows:
Append commands to the history file, rather than overwrite it.
$ shopt -s histappend
Save each command right after it has been executed, not at the end of the session.
$ PROMPT_COMMAND='$PROMPT_COMMAND; history -a; history -n'
Insert the 2 statements into ~/.bashrc
shopt -s histappend
PROMPT_COMMAND='$PROMPT_COMMAND; history -a; history -n'
If you already don't have $PROMPT_COMMAND set then just use:
shopt -s histappend
PROMPT_COMMAND='history -a; history -n'
How do you test it out:
- After setting the above in .bashrc; open 2 sessions.
- In session1 execute any command
- In session2 ... just press "Enter" and then "up" arrow to see the commands from session1.