Ask Your Question
6

How to keep history commands in sync across multiple sessions ?

asked 2011-09-30 01:33:42 -0600

shanks gravatar image

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. But how do you maintain history commands across multiple sessions ?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
4

answered 2011-09-30 01:38:02 -0600

shanks gravatar image

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:

  1. After setting the above in .bashrc; open 2 sessions.
  2. In session1 execute any command
  3. In session2 ... just press "Enter" and then "up" arrow to see the commands from session1.
edit flag offensive delete link more

Comments

Your PROMPT_COMMAND syntax fails for me (the semi, I think)… This works: PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''history -a; history -n'

paulirish gravatar imagepaulirish ( 2015-04-16 14:12:51 -0600 )edit

Also I get duplicates like the other commenter.

paulirish gravatar imagepaulirish ( 2015-04-16 14:16:10 -0600 )edit
1

answered 2011-11-15 14:17:17 -0600

Peter Scott gravatar image

updated 2011-12-21 06:51:58 -0600

Here is a .bashrc file that will synchronise the history between all bash instances:

# .bashrc - bash startup file
#
# Mon Oct 3 21:44:00 BST 2011
#

history(){
  syncHistory
  builtin history "$@"
}

syncHistory(){
  builtin history -a
  HISTFILESIZE=$HISTFILESIZE
  builtin history -c
  builtin history -r
}

promptCommand(){
  if [ "$TERM" = xterm ]
  then case "$DISPLAY" in
       :*)  printf "\033]0;%s\007"                     "$PWD" ;;
       *)   printf "\033]0;%s -- %s\007" "$HOST_UPPER" "$PWD" ;;
       esac
  fi
  syncHistory
}

HOST_UPPER=`echo $HOSTNAME | tr '[a-z]' '[A-Z]'`
PROMPT_COMMAND=promptCommand

It also dispays the current directory name in the terminal's titlebar. You can put your usual .bashrc stuff at the end of the file.

Hope this helps!

Peter Scott

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2011-09-30 01:33:42 -0600

Seen: 6,424 times

Last updated: Dec 21 '11