The history command in Linux and macOS displays a list of previously executed commands, letting you review, reuse, and manage your shell history. You can recall any past command with ! shortcuts, search with Ctrl+R, and clear or modify the history file. This guide covers the most useful history command examples with practical explanations.
What the history Command Does
When you type commands in a terminal, your shell (usually Bash or Zsh) stores them in memory and writes them to a history file (typically ~/.bash_history or ~/.zsh_history). The history command prints that list with line numbers, so you can reference commands by number. It also accepts options to control how much history is shown, search for specific entries, and delete unwanted lines.
Basic history Command Examples
history– Show the entire history list with line numbers.history 20– Display only the last 20 commands.history -5– Also shows the last 5 commands (same ashistory 5).history -c– Clear the current session’s history (does not delete the file).history -w– Write the in-memory history to the history file immediately.history -a– Append new commands to the history file without overwriting.
Re-running Commands with ! Shortcuts
The exclamation mark (!) gives you quick access to previous commands without typing them again.
!!– Run the last command again. Useful after a forgottensudo.!10– Run command number 10 from the history list.!-3– Run the command three entries back.!ls– Run the most recent command that starts with “ls”.!?pattern?– Run the most recent command containing “pattern”.^old^new– Quickly replace “old” with “new” in the last command and run it. For example,^cat^dogchangescattodogin the previous command.
Searching and Filtering History
Instead of scrolling through hundreds of lines, you can search directly.
history | grep ssh– Pipe the output togrepto find commands containing “ssh”.history | grep -i 'rm -rf'– Case-insensitive search for dangerous commands.Ctrl+R– Interactive reverse search. Start typing and the shell shows the most recent match; pressCtrl+Ragain to cycle backward.fc -l 10 20– List commands 10 through 20 (thefccommand is a flexible history tool).
Controlling What Gets Saved
You can prevent sensitive or trivial commands from being recorded, and control the size of your history.
HISTSIZE=500– Number of commands kept in the current session (default is often 1000).HISTFILESIZE=2000– Maximum lines in the history file.HISTCONTROL=ignoreboth– Ignore commands that start with a space and duplicate commands.HISTIGNORE='ls:cd:pwd'– Do not save these commands.- Add a leading space before a command (e.g.,
secret-command) to prevent it from being saved ifHISTCONTROLincludesignorespace.
Common Problems and Fixes
History not saved after closing the terminal? In some configurations, the shell only writes history when you exit gracefully. Use history -w to force a write, or set PROMPT_COMMAND="history -a" in your .bashrc to append after every command.
Duplicate commands cluttering the list? Set HISTCONTROL=erasedups to remove older duplicates, or ignoredups to skip consecutive duplicates.
History lost after a crash? If the terminal crashes, the in-memory history is lost. Adding shopt -s histappend to your .bashrc makes the shell append (instead of overwrite) to the history file, reducing data loss.
Comparison: Bash vs Zsh vs Fish
The history command works similarly in Bash and Zsh, but there are differences:
- Bash – Uses
~/.bash_history, supports!shortcuts, andCtrl+Rsearch. - Zsh – Uses
~/.zsh_history, supports the same shortcuts, plus shared history across terminals if configured. - Fish – Has no
historycommand; usehistoryto view,history searchto find, andhistory deleteto remove entries. Fish also has autosuggestions built in.
Safety and Privacy Considerations
Your history file can contain passwords, API keys, or other sensitive data if you typed them directly. To protect yourself:
- Use
history -cto clear the current session andhistory -wto overwrite the file. - Delete specific entries with
history -d 123(replace 123 with the line number). - Edit the history file manually with
nano ~/.bash_historyto remove sensitive lines. - Set
HISTCONTROL=ignorespaceand always prefix sensitive commands with a space.
Frequently Asked Questions
How do I see the full history without line numbers?
Use history | cut -c8- to strip the line numbers, or simply open the history file with cat ~/.bash_history. Note that the file may not include commands from the current session until you run history -w.
Why does my history show the same command multiple times?
By default, Bash saves every command unless you set HISTCONTROL to ignore duplicates. Add HISTCONTROL=ignoreboth to your .bashrc to ignore duplicates and leading-space commands.
Can I search history with a fuzzy match?
Yes, if you use fzf (fuzzy finder) or Ctrl+R with a fuzzy-finding plugin. Alternatively, you can pipe history to grep with a pattern like history | grep 'git.*commit'.
How do I clear history for the current session only?
Run history -c. This clears the in-memory list, but the history file remains unchanged until you run history -w (which would overwrite it with the now-empty list).
Is there a way to avoid saving any history at all?
Set HISTSIZE=0 and HISTFILESIZE=0 in your shell configuration. This prevents both the session list and the file from growing. Alternatively, use unset HISTFILE to stop writing to the file entirely.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Live Location Sharing for Meeting Strangers: Safe or Not?
- Why Keep OLED Switch After Buying Switch 2
- Why I Want More Sequels to Bad Games
- Highest Mileage Tesla Model 3: Record Holders
- Scanned Network Found Unknown Devices? Security Risks
- Old Game Graphics: Why They Look Worse Than You Remember