---
title: "Per-App Shell History for Bash"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/bash-per-app-history
---

![Blog post image for Per-App Shell History for Bash - Keep your Bash history organized across different terminal applications. This snippet automatically creates separate history files for each terminal emulator you use.](/_astro/hero.Da_6jPH6_Z1YxJaV.webp)

[Home](/)›[Codesnippets](/codesnippets)›[All Categories](/codesnippets/categories)›[Shell scripting](/codesnippets/categories/shell-scripting)

Codesnippets

[Next in Shell scriptingBash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/codesnippets/post/bash-retry-function-exponential-backoff)

[Shell scripting](/codesnippets/categories/shell-scripting)[Productivity](/codesnippets/categories/productivity)[Bash](/codesnippets/bash)

# Per-App Shell History for Bash

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 18 Dec 202502 Mins read03 Mins listen

[Markdown for AI(opens in a new tab)](/post/bash-per-app-history/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

Keep your Bash history organized across different terminal applications. This snippet automatically creates separate history files for each terminal emulator you use.

Series

[Shell Mastery](/series/shell-mastery)1/2

[NextPer-App Shell History for Zsh](/codesnippets/post/zsh-per-app-history)

All posts in this series (2)

Code Snippets2

1.  [Per-App Shell History for BashYou are here](/codesnippets/post/bash-per-app-history)
2.  [Per-App Shell History for Zsh](/codesnippets/post/zsh-per-app-history)

### Per-App Shell History for Bash

Contents

[The Problem](#the-problem)[Mixed command history](#mixed-command-history)[Context switching issues](#context-switching-issues)[The Solution](#the-solution)[Per-app history files](#per-app-history-files)[Automatic organization](#automatic-organization)[Implementation](#implementation)[Bash function setup](#bash-function-setup)[Benefits and usage](#benefits-and-usage)[Organization advantages](#organization-advantages)[Managing history files](#managing-history-files)[Community Discussion](#community-discussion)[Your history management](#your-history-management)[Alternative approaches](#alternative-approaches)

**Organize your Bash history per terminal app.**

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](#the-problem)

### [Mixed command history](#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](#context-switching-issues)

Different terminals used for different purposes (work, personal, testing) end up with mixed command histories, which makes context hard to keep.

## [The Solution](#the-solution)

### [Per-app history files](#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. It all happens on shell startup.

### [Automatic organization](#automatic-organization)

**TL;DR**

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

## [Implementation](#implementation)

### [Bash function setup](#bash-function-setup)

~/.bashrc

```
1function set_app_history() {2
3  # Get terminal app name (fallback: Apple_Terminal)4  local app="${TERM_PROGRAM:-Apple_Terminal}"5
6  # Default history file7  HISTFILE="$HOME/.bash/.bash_history"8
9  # Ghostty on Linux / WSL → use default history10  if [[ "$app" == "Ghostty" && "$(uname -s)" == "Linux" ]]; then11    export HISTFILE12    return13  fi14
15  # Apple Terminal → use default history16  if [[ "$app" == "Apple_Terminal" ]]; then17    export HISTFILE18    return19  fi20
21  # Everything else → per-app history22  app="${app//[^A-Za-z0-9]/_}"23  HISTFILE="$HOME/.bash/.bash_history_${app}"24  export HISTFILE25}26
27set_app_history
```

## [Benefits and usage](#benefits-and-usage)

### [Organization advantages](#organization-advantages)

**Why This Helps**

This approach keeps your shell history organized without any manual work. You can switch between terminals without worrying about command pollution or losing context. It helps most if you use different terminals for work, personal projects, or testing, since each gets its own clean slate.

### [Managing history files](#managing-history-files)

**Bonus Tip**

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

Terminal window

```
1ls -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](#community-discussion)

### [Your history management](#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](#alternative-approaches)

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

Was this useful?

## Tags

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Productivity](/codesnippets/tags/productivity)[#DevTools](/codesnippets/tags/devtools)[#Terminal](/codesnippets/tags/terminal)[#Configuration](/codesnippets/tags/configuration)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=Per-App%20Shell%20History%20for%20Bash&url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history&title=Per-App%20Shell%20History%20for%20Bash&summary=Keep%20your%20Bash%20history%20organized%20across%20different%20terminal%20applications.%20This%20snippet%20automatically%20creates%20separate%20history%20files%20for%20each%20terminal%20emulator%20you%20use.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=Per-App%20Shell%20History%20for%20Bash%20https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history&text=Per-App%20Shell%20History%20for%20Bash "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history&title=Per-App%20Shell%20History%20for%20Bash "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history&t=Per-App%20Shell%20History%20for%20Bash "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history&media=&description=Keep%20your%20Bash%20history%20organized%20across%20different%20terminal%20applications.%20This%20snippet%20automatically%20creates%20separate%20history%20files%20for%20each%20terminal%20emulator%20you%20use. "Share on Pinterest")[Email](<mailto:?subject=Per-App%20Shell%20History%20for%20Bash&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fbash-per-app-history>)

## Comments

## You might also enjoy

More posts on similar topics

[![Per-App Shell History for Zsh](/_astro/hero.DRenzVy__1xwzSL.webp)](/codesnippets/post/zsh-per-app-history)

## [Per-App Shell History for Zsh](/codesnippets/post/zsh-per-app-history)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Productivity](/codesnippets/categories/productivity)

Organize your shell history per terminal app. 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 b

[#Zsh](/codesnippets/tags/zsh)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Productivity](/codesnippets/tags/productivity)+3 tags

[read more](/codesnippets/post/zsh-per-app-history)

[![Essential Bash Variables for Every Script](/_astro/hero.B4bDowyY_YQpD7.webp)](/codesnippets/post/essential-bash-variables)

## [Essential Bash Variables for Every Script](/codesnippets/post/essential-bash-variables)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)

Overview Quick Tip You know what's worse than writing scripts? Writing scripts that break every time you move them to a different machine. Built-in Bash variables fix that. The problem wi

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Linux](/codesnippets/tags/linux)+4 tags

[read more](/codesnippets/post/essential-bash-variables)

[![Why printf Beats echo in Linux Scripts](/_astro/hero.Dl3YkIwZ_Z1w9hux.webp)](/codesnippets/post/printf-beats-echo-linux-scripts)

## [Why printf Beats echo in Linux Scripts](/codesnippets/post/printf-beats-echo-linux-scripts)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)
-   [Linux](/codesnippets/categories/linux)

Scripting Tip A script that works on your machine can produce different output on another system. The output command is often the reason. printf behaves the same way across shells, and echo d

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Printf](/codesnippets/tags/printf)+5 tags

[read more](/codesnippets/post/printf-beats-echo-linux-scripts)

[![Check S3 Bucket Existence](/_astro/hero.CWiO4GWV_1YUYTb.webp)](/codesnippets/post/bash-s3-bucket-exists)

## [Check S3 Bucket Existence](/codesnippets/post/bash-s3-bucket-exists)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Aws](/codesnippets/categories/aws)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)

Quick Tip Don't let your deployment blow up because of a missing S3 bucket. This Bash script lets you check if a bucket exists before anything fails. The Problem Missing bucket failure

[#Bash](/codesnippets/tags/bash)[#AWS](/codesnippets/tags/aws)[#DevOps](/codesnippets/tags/devops)+3 tags

[read more](/codesnippets/post/bash-s3-bucket-exists)

[![Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/_astro/hero.Dap62rVN_Z1q0nar.webp)](/codesnippets/post/bash-retry-function-exponential-backoff)

## [Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/codesnippets/post/bash-retry-function-exponential-backoff)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Devops](/codesnippets/categories/devops)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Automation](/codesnippets/categories/automation)

Quick Tip Wrap any flaky command in one reusable retry function and stop re-running red pipelines by hand. The Problem The Problem Some commands fail for reasons that have nothing to

[#Bash](/codesnippets/tags/bash)[#Retry](/codesnippets/tags/retry)[#Exponential Backoff](/codesnippets/tags/exponential-backoff)+3 tags

[read more](/codesnippets/post/bash-retry-function-exponential-backoff)

[![Optimizing your python code with \_\_slots\_\_?](/_astro/hero.DP_vYsHU_gYDXn.webp)](/codesnippets/post/python-slots-optimization)

## [Optimizing your python code with \_\_slots\_\_?](/codesnippets/post/python-slots-optimization)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Python](/codesnippets/categories/python)
-   [Productivity](/codesnippets/categories/productivity)

Memory optimization with slots Understanding the problem Optimizing data models in big data workflows with slots In big data and MLOps workflows, you often work with massive

[#Python](/codesnippets/tags/python)[#MemoryOptimization](/codesnippets/tags/memoryoptimization)[#DataScience](/codesnippets/tags/datascience)+4 tags

[read more](/codesnippets/post/python-slots-optimization)

6 related posts
