[Fixed] zsh: corrupt history file ~/.zsh_history on macOS & Linux: Step-by-Step Troubleshooting Guide

Quick Summary: The error zsh: corrupt history file /Users/<user>/.zsh_history (or /home/<user>/.zsh_history) occurs in Z shell on macOS and Linux when the shell attempts to read its command history file at startup and encounters invalid UTF-8 bytes, null characters (\0), or truncated lines. This typically happens after an unexpected system crash, sudden power loss, full disk error, or concurrent writes from multiple terminal tabs.

Understanding the Root Causes

Zsh stores command timestamps and multi-line commands in ~/.zsh_history using an internal extended metafied encoding format. Because the shell appends to this file upon session exit (or immediately when INC_APPEND_HISTORY / SHARE_HISTORY is enabled), any interruption mid-write leaves malformed byte sequences behind. Common causes include:

  • Abrupt Shutdown or Kernel Panic: A forced reboot, battery drain, or killing Terminal/iTerm2 mid-flush writes partial lines or null bytes into ~/.zsh_history.
  • Non-UTF-8 Binary Output Pasted into Terminal: Accidentally pasting binary characters or invalid byte sequences at the prompt, which Zsh then saves to history.
  • Concurrent Session Race Conditions: Multiple terminal windows or tmux panes writing to ~/.zsh_history simultaneously without file locking enabled.

Step 1: Quick Fix – Recover Readable Commands Using strings

Rather than deleting your entire command history, back up the corrupted file and extract all valid printable lines using the built-in strings utility and Zsh’s fc -R reload command:

# 1. Move the corrupted history file to a backup
mv ~/.zsh_history ~/.zsh_history_bad

# 2. Extract valid ASCII/UTF-8 text lines into a clean history file
strings -eS ~/.zsh_history_bad > ~/.zsh_history
# Note: On macOS BSD strings, if -eS is unsupported, simply run:
# strings ~/.zsh_history_bad > ~/.zsh_history

# 3. Reload the repaired history into your current Zsh session
fc -R ~/.zsh_history

# 4. Remove the backup once you confirm your history is intact
rm ~/.zsh_history_bad

Step 2: Preserve Extended Unicode / Non-ASCII Characters with iconv

The standard strings command strips non-ASCII characters (such as accented letters or Unicode arguments). If your commands contain international characters, use iconv with -c to strip only invalid byte sequences while keeping valid UTF-8 text intact:

# 1. Back up the corrupted history file
cp ~/.zsh_history ~/.zsh_history_bak

# 2. Filter out invalid byte sequences while preserving UTF-8 characters
iconv -f UTF-8 -t UTF-8 -c ~/.zsh_history_bak > ~/.zsh_history_clean

# 3. Replace the original file and reload history
mv ~/.zsh_history_clean ~/.zsh_history
fc -R ~/.zsh_history

Step 3: Prevent Future Corruption in ~/.zshrc

Enable Zsh’s built-in history file locking and atomic write options in ~/.zshrc so concurrent terminal tabs and sudden crashes do not corrupt your history again:

# Edit your ~/.zshrc configuration
nano ~/.zshrc

# Add or update these history safety options:
setopt HIST_FCNTL_LOCK      # Use OS file locking when writing history
setopt INC_APPEND_HISTORY   # Write commands incrementally instead of all at exit
setopt HIST_IGNORE_DUPS     # Avoid redundant duplicate entries
HISTSIZE=50000
SAVEHIST=50000

# Apply the updated configuration
source ~/.zshrc

Verification & Testing Steps

Open a new terminal tab or restart Zsh to confirm that the startup warning is gone and your past commands remain searchable:

# 1. Start a fresh Zsh login shell and check for zero warnings
zsh -l -c "echo 'Zsh started cleanly'"

# 2. Verify your recovered command history is loaded
fc -l -15

# 3. Confirm history file permissions and non-zero size
ls -lh ~/.zsh_history

Troubleshooting Summary Comparison

Scenario Primary Root Cause Recommended Solution
Standard ASCII command history Null bytes or truncated lines after crash Recover with strings ~/.zsh_history_bad > ~/.zsh_history
History includes UTF-8 / non-English text strings strips valid multibyte characters Filter invalid bytes using iconv -f UTF-8 -t UTF-8 -c
Corruption recurs with tmux / many tabs Simultaneous uncoordinated writes to history file Enable setopt HIST_FCNTL_LOCK in ~/.zshrc
History file completely unreadable (0 bytes) Disk full or severe filesystem truncation Remove ~/.zsh_history and touch a fresh file

Leave a Reply