Preserve Bash History in Multiple Terminal Windows on Linux

Bash history is a powerful tool that can help you keep track of all the commands you've executed in your terminal. It can be especially useful when you're working with multiple terminal windows, as it allows you to easily switch between them and pick up where you left off. In this article, we'll show you how to preserve bash history in multiple terminal windows on Linux, and explain why it's so important.

Why Preserve Bash History?

Preserving bash history is important because it allows users to easily recall commands they have previously executed in the terminal. This can save time and effort by avoiding the need to re-enter the same commands over and over again.

Additionally, preserving bash history can help users troubleshoot any issues they may be having with their system or software. By reviewing the commands that have been executed, users can identify any mistakes or errors that may have occurred.

Furthermore, preserving bash history can serve as a reference for future use. By having a record of the commands executed, users can refer back to them in case they need to replicate a previous action or troubleshoot a similar problem.

The Multiple Terminal Problem

By default, bash history only saves commands from the last terminal session that was closed. When you have multiple terminal windows open simultaneously, commands from other windows are lost when they close. This creates several issues:

  • Command Loss Commands executed in terminal windows that close first are not saved

  • Incomplete History Your command history becomes fragmented across sessions

  • Reduced Productivity You lose valuable commands that could be reused

Configuration Methods

Method 1: Real-time History Sharing

To enable real-time history sharing across all terminal windows, add the following lines to your ~/.bashrc file:

# Append to history instead of overwriting
shopt -s histappend

# Save history after each command
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

# Increase history size
HISTSIZE=10000
HISTFILESIZE=20000

Method 2: Basic History Preservation

For a simpler approach that saves history from all sessions (but doesn't share in real-time), use:

# Enable history appending
shopt -s histappend

# Set history sizes
HISTSIZE=5000
HISTFILESIZE=10000

# Save history immediately
PROMPT_COMMAND="history -a"

Method 3: Advanced Configuration with Timestamps

For enhanced history tracking with timestamps:

# History settings
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTCONTROL=ignoredups:ignorespace

# Append and sync history
shopt -s histappend
PROMPT_COMMAND="history -a; history -c; history -r"

Key Configuration Options

Option Purpose Example Value
HISTSIZE Commands stored in memory 10000
HISTFILESIZE Commands stored in .bash_history file 20000
HISTTIMEFORMAT Timestamp format for commands "%Y-%m-%d %H:%M:%S "
HISTCONTROL Filter duplicate/space-prefixed commands ignoredups:ignorespace

Applying the Configuration

After editing your ~/.bashrc file, apply the changes using one of these methods:

# Reload bashrc in current terminal
source ~/.bashrc

# Or restart your terminal session

For existing terminal windows, you'll need to run source ~/.bashrc in each one to apply the new settings.

Verification

To verify your configuration is working:

# Check current settings
echo $HISTSIZE
echo $HISTFILESIZE
shopt | grep histappend

# Test history sharing (open multiple terminals and run commands)
history | tail -5

Conclusion

Preserving bash history across multiple terminal windows significantly improves productivity and command recall capabilities. The histappend option combined with proper PROMPT_COMMAND configuration ensures that all your commands are preserved and optionally shared in real-time across sessions.

Updated on: 2026-03-17T09:01:38+05:30

648 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements