🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

history Command

Beginner Shell Scripting man(1)

Display or manipulate command history

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
history [OPTION]... [N]

What Does history Do?

history displays the command history list, showing previously executed commands with their line numbers. It allows you to search, recall, and re-execute past commands, dramatically increasing shell productivity.

Bash stores command history in memory during a session and saves it to ~/.bash_history on exit. The HISTSIZE variable controls how many commands are kept in memory, and HISTFILESIZE controls the file size.

history supports searching (Ctrl+R), re-execution (!N, !!), and editing. Mastering history navigation is one of the biggest productivity gains for Linux users.

Options & Flags

OptionDescriptionExample
N Show last N commands history 20
-c Clear the history list history -c
-d N Delete entry number N history -d 150
-w Write history to file history -w
-r Read history from file history -r
-a Append new entries to history file history -a

Practical Examples

#1 Show recent commands

Shows the last 20 commands you executed.
$ history 20
Output: 151 git status\n 152 git add .\n 153 git commit -m "fix"

#2 Search history

Finds all SSH commands you have previously run.
$ history | grep ssh
Output: 45 ssh user@server1\n 89 ssh admin@server2

#3 Re-execute last command

Runs the previous command again. Useful for: sudo !!
$ !!

#4 Re-execute by number

Runs the command at history line 151.
$ !151

#5 Re-execute by prefix

Runs the most recent command starting with "ssh".
$ !ssh

#6 Reverse search

Interactive reverse search — type to find matching commands.
$ Ctrl+R then type search term

#7 Clear history

Clears history in memory and overwrites the history file.
$ history -c && history -w

#8 Delete specific entry

Removes a specific command from history (useful for passwords accidentally typed).
$ history -d 150

Tips & Best Practices

Ctrl+R reverse search: Press Ctrl+R and type to search backwards through history. Press Ctrl+R again to find older matches. Enter to execute, Ctrl+G to cancel.
sudo !! pattern: Forgot sudo? Type sudo !! to re-run the last command with sudo. !! is replaced with the previous command.
Sensitive data in history: Commands with passwords appear in history. Use HISTCONTROL=ignorespace and prefix sensitive commands with a space to exclude them.

Frequently Asked Questions

How do I search command history?
Press Ctrl+R for interactive search, or use history | grep pattern for all matches.
How do I repeat the last command?
Type !! to repeat the last command. Use !N for a specific history number. Use !prefix for the last command starting with prefix.
How do I prevent a command from being saved?
Set HISTCONTROL=ignorespace in .bashrc, then prefix commands with a space: " secret-command" (note the leading space).

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →