Print and Query Command History to Find Past Commands
In this article
You can print and query your command history to find past commands using the history command in most Unix-like shells (bash, zsh) and Get-History in PowerShell. These tools let you review, search, and re-run previously executed commands, saving time and reducing errors. Below, we explore how history works, how to search it effectively, and common pitfalls.
What Is Command History?
Command history is a feature of interactive shells that records the commands you type. When you press Enter, the shell stores that command in memory and, when the session ends, appends it to a history file (e.g., ~/.bash_history for bash, ~/.zsh_history for zsh). This allows you to recall and reuse commands without retyping them. The history is per-user and typically not shared across users or machines unless you configure it.
How to Print the Full History
To see all recorded commands, simply run:
- bash/zsh:
history(displays numbered list) - PowerShell:
Get-History(displays with IDs)
In bash, you can also use history 20 to show only the last 20 entries. In PowerShell, Get-History | Select-Object -Last 20 does the same. The output includes a line number (or ID) that you can use to re-run a command with !number (bash) or Invoke-History -Id number (PowerShell).
Searching History with grep and Select-String
To find a specific command, pipe the history output into a search tool:
- bash/zsh:
history | grep "git commit"(case-sensitive; usegrep -ifor case-insensitive) - PowerShell:
Get-History | Select-String "git commit"
You can also search the history file directly: grep "pattern" ~/.bash_history. For more complex patterns, use regular expressions, e.g., history | grep -E "^[0-9]+ (sudo|apt)" to find commands starting with sudo or apt.
Interactive Reverse Search (Ctrl+R)
Instead of printing history, you can interactively search it. In bash and zsh, press Ctrl+R and start typing; the shell shows the most recent matching command. Press Ctrl+R again to cycle through older matches. Press Enter to execute, or Esc to edit. PowerShell offers a similar feature with F8 (or Ctrl+R in PSReadLine). This method is faster for quick lookups because it avoids printing the entire history.
Common Problems and Solutions
Several issues can arise when working with history:
- History not saved across sessions: Ensure your shell is configured to append history. In bash, set
shopt -s histappendandPROMPT_COMMAND="history -a"in your~/.bashrc. For zsh, usesetopt appendhistory. - Duplicate commands clutter output: Use
history | grep "pattern" | sort -uto remove duplicates, or setHISTCONTROL=ignoredupsin bash. - History truncated: Increase the history size with
HISTSIZE=10000andHISTFILESIZE=20000in bash; in zsh, useHISTSIZE=10000andSAVEHIST=10000. - Commands with spaces not saved: By default, bash ignores commands starting with spaces (to avoid saving secrets). If you want to save them, set
HISTCONTROL=ignorespace(orignoreboth) to keep them.
Comparing History Features Across Shells
While the core idea is the same, implementations differ:
- bash: Uses
history, supports!number,!!(last command), and!$(last argument). History file is plain text. - zsh: Similar to bash but adds
fc -lfor listing, and supports shared history across multiple terminals if configured. - PowerShell: Uses
Get-HistoryandInvoke-History. History is stored in memory and optionally saved to a file usingExport-History. PSReadLine module enhances it with predictive suggestions. - fish: No
historycommand by default; usehistory searchorhistory | grep.
Safety and Privacy Considerations
Command history can contain sensitive information like passwords or API keys. Be cautious when sharing terminal output. To avoid recording sensitive commands, prefix them with a space (if configured) or use unset HISTFILE before running them. You can also delete specific entries with history -d line_number (bash) or clear the entire history with history -c. In PowerShell, use Clear-History. Always review what you are about to print if you are screen-sharing.
Frequently Asked Questions
How do I search history for a command I ran yesterday?
Use history | grep "pattern" to find it. If the history file is large, you can also search the file directly: grep "pattern" ~/.bash_history. For zsh, the file is ~/.zsh_history. If you know the approximate time, you can use history | tail -n 100 to see recent entries.
Why does my history not show commands from other terminals?
By default, each terminal session has its own history buffer, and they only merge when the shell exits. To share history across terminals in bash, add shopt -s histappend and PROMPT_COMMAND="history -a" to your ~/.bashrc. For zsh, use setopt sharehistory.
Can I prevent a command from being saved to history?
Yes. In bash and zsh, prefix the command with a space (e.g., secret-command) if HISTCONTROL=ignorespace is set. Alternatively, run unset HISTFILE before the command to disable saving for the session, or use history -d line_number to delete it afterward.
What is the difference between history and fc -l?
In bash, fc -l (list) is similar to history but offers more formatting options, such as fc -l 10 20 to show a range. In zsh, fc -l is the primary way to list history, and history is an alias for it. Both display numbered entries.
How do I re-run a command from history without retyping it?
In bash/zsh, type !number (e.g., !42) to run that command. Use !! for the last command, and !$ for the last argument of the last command. In PowerShell, use Invoke-History -Id 42 or simply press Up arrow to cycle through history.
Is there a way to search history with a fuzzy match?
Yes, tools like fzf (fuzzy finder) integrate with shells to provide interactive fuzzy search. Install fzf and bind it to Ctrl+R in your shell configuration. This allows you to type a partial string and see a list of matches, which you can select with arrow keys.