Alines Webマガジン

bash history

📅 October 29, 2026📖 Verified Publication
```html Bash History: The Complete Guide to Command History Management, Security & Best Practices

Bash History: The Complete Guide to Command History Management, Security & Best Practices

Last updated: July 2026 · 14 min read

Key Takeaways

  • Bash history is stored by default in ~/.bash_history — a plain-text file that records every command you execute in an interactive shell session.
  • History behavior is controlled by four core variables: HISTCONTROL, HISTSIZE, HISTFILESIZE, and HISTTIMEFORMAT.
  • Security risk is real: passwords, API keys, and tokens entered in plain text are persisted indefinitely unless explicitly filtered or disabled.
  • You can export bash history as JSON using the hist tool or by parsing ~/.bash_history with standard Unix utilities.
  • Advanced users leverage fc, history expansion (!!, !$, !n), and time-stamped history for dramatic productivity gains.
  • Multi-session sync requires shopt -s histappend and periodic history -a to avoid overwriting concurrent sessions.

What Is Bash History?

Bash history is one of the most underappreciated features of the GNU Bash shell. Since its introduction in the 1989 BSD shell, command history has been a cornerstone of Unix-like interactivity — and Bash preserves and extends that capability far beyond simple recall.

By default, Bash stores the command history of each interactive session in the file ~/.bash_history located in the user's home directory. When the shell exits, it appends the session's commands to this file. On the next login, the entire history is loaded into memory, allowing you to navigate upward and downward through previously executed commands using Ctrl+P / Ctrl+N or the up/down arrow keys.

The history mechanism operates on two levels: in-memory history (the current session's commands) and on-disk history (the persistent .bash_history file). Understanding how these two layers interact — and how to control their synchronization — is essential for both productivity and security.

How Bash History Works Under the Hood

When an interactive Bash shell starts, it reads its configuration files in a specific order:

  1. /etc/bash.bashrc (system-wide)
  2. ~/.bashrc (per-user)
  3. ~/.bash_profile or ~/.bash_login or ~/.profile

The history file path and size limits are governed by environment variables evaluated during this initialization phase. If HISTFILE is unset, Bash defaults to ~/.bash_history. If HISTSIZE is unset, the default is 500 entries in memory; if HISTFILESIZE is unset, the default is 500 lines on disk.

Crucially, history is not automatically written to disk during a session. By default, Bash flushes the entire in-memory history to ~/.bash_history only when the shell exits. This design choice creates a well-known race condition in multi-window or multi-terminal scenarios, which we address in the best-practices section below.

The Core Configuration Variables

Four environment variables govern every aspect of Bash history behavior. Getting these right is the difference between a well-oiled workflow and a fragile, leaky shell session.

© 2026 Alines Webマガジン. All rights reserved.

Variable Purpose Default Value Recommended Value
HISTFILE Path to the history file ~/.bash_history ~/.bash_history (unchanged)