Understanding and efficiently using Bash history helps speed up workflow, avoid retyping, and even prevent mistakes.
๐ Where is Bash History Stored?
-
History is saved in:
~/.bash_history -
Two environment variables control its behavior:
Variable
Description
Example
HISTFILESIZEMax number of commands in the
.bash_historyfileecho $HISTFILESIZEโ2000HISTSIZEMax number of commands kept in memory (used by
historycommand)echo $HISTSIZEโ1000
โ ๏ธ .bash_history is only updated after logout.
๐ Viewing History
history # Show recent commands (from memory, not full file)
cat ~/.bash_history # Show saved command history from the file
๐ Rerunning Commands
Syntax | Action |
|---|---|
| Run the command by its history number (e.g., |
| Run the last command |
| Run the command that was |
| Run the most recent command that starts with |
| Print the matched command without executing it |
๐ Navigating History
Shortcut | Action |
|---|---|
| Previous command |
| Next command |
| Reverse search in history (interactive) |
| Exit reverse search without executing |
| Run the found command |
โ Example:
Ctrl + R โ type 'ping' โ press Enter โ runs last 'ping' command
Ctrl + R โ type 'cat' โ Ctrl + G โ exits search mode
๐งน Cleaning History
Command | Action |
|---|---|
| Delete a specific command by its number |
| Clear the entire session history |