Per-App Shell History for Bash

Terminal Chaos? Organize Your Bash History!

Ever jumped between iTerm2, Ghostty, and VS Code’s terminal only to have your command history get all mixed up? This Bash snippet keeps things clean by creating separate history files for each terminal app you use.

The Problem

Mixed Command History

When you use multiple terminal emulators (iTerm, Alacritty, Ghostty, VS Code, etc.), they all share the same shell history file by default. This means commands you ran in one app show up when you press the up arrow in another. It gets messy fast, especially when you use different terminals for different projects or contexts.

Context Switching Issues

Different terminals used for different purposes (work, personal, testing) end up with mixed command histories, making it difficult to maintain context.

The Solution

Per-App History Files

This Bash function detects which terminal application you’re using and automatically assigns a dedicated history file for it. Apple Terminal and Ghostty on Linux get the default history, while everything else gets its own isolated file. Clean, simple, and automatic.

Automatic Organization

TL;DR

  • Automatically creates separate history files for each terminal app.
  • Keeps Apple Terminal and Ghostty (Linux) on the default history.
  • Works seamlessly with iTerm2, Alacritty, Warp, Kitty, and more.
  • Just add it to your ~/.bashrc or ~/.bash_profile and forget about it.

Implementation

Bash Function Setup

~/.bashrc
function set_app_history() {
# Get terminal app name (fallback: Apple_Terminal)
local app="${TERM_PROGRAM:-Apple_Terminal}"
# Default history file
HISTFILE="$HOME/.bash/.bash_history"
# Ghostty on Linux / WSL → use default history
if [[ "$app" == "Ghostty" && "$(uname -s)" == "Linux" ]]; then
export HISTFILE
return
fi
# Apple Terminal → use default history
if [[ "$app" == "Apple_Terminal" ]]; then
export HISTFILE
return
fi
# Everything else → per-app history
app="${app//[^A-Za-z0-9]/_}"
HISTFILE="$HOME/.bash/.bash_history_${app}"
export HISTFILE
}
set_app_history

Benefits and Usage

Organization Advantages

Why This Helps

This approach keeps your shell history organized without any manual intervention. You can switch between terminals without worrying about command pollution or losing context. Plus, it’s especially useful if you use different terminals for work, personal projects, or testing each gets its own clean slate.

Managing History Files

Bonus Tip

Want to see which history files you have? Run this:

Terminal window
ls -lh ~/.bash/.bash_history*

You’ll see all your per-app history files. Each one is tied to a specific terminal emulator, so you can even back them up or clean them individually.

Community Discussion

Your History Management

Your Turn

How do you organize your shell history? Do you use per-app separation or something else? Drop your setup below!

Alternative Approaches

Share your favorite methods for keeping shell history organized across different terminal applications.

Related Posts

Check out some of our other posts

Per-App Shell History for Zsh

Terminal Chaos? Organize Your Shell History! Ever jumped between iTerm2, Ghostty, and VS Code's terminal only to have your command history get all mixed up? This Zsh snippet keeps things clean by

Essential Bash Variables for Every Script

Overview Quick Tip You know what's worse than writing scripts? Writing scripts that break every time you move them to a different machine. Let's fix that with some built-in Bash variables tha

Why printf Beats echo in Linux Scripts

Scripting Tip You know that feeling when a script works perfectly on your machine but fails miserably somewhere else? That's probably because you're using echo for output. Let me show you why pri

Check S3 Bucket Existence

'Cloud' 'Automation' isCodeSnippet: true draft: falseQuick Tip Don’t let your deployment blow up because of a missing S3 bucket. This Bash script lets you check if a bucket exists

Optimizing your python code with __slots__?

Memory Optimization with slots Understanding the Problem Dev Tip: Optimizing Data Models in Big Data Workflows with slots In big data and MLOps workflows, you often work with