📜 How to Use Linux Bash History
Developers, sysadmins, and DevOps engineers spend a lot of time at the Linux command line. Inevitably, we make typos, need to rerun commands, or check previous actions. Knowing how to work with Linux bash history can help in all these cases.
In this article, we’ll explore what bash history is, how it works, and how you can use it to be more efficient when working at the Linux command line.
🔍 What is Bash’s History?
Bash history refers to the commands, files, and shortcuts that let you view, modify, and delete commands that have been run on a system.
Bash has two built-in commands for working with history:
history– Lists commands and modifies bash history.fc– Lists, edits, and executes commands from bash history.
We’ll focus on the history command in this article.
📁 Where is Bash History Stored?
By default, bash stores commands in RAM until you log out, after which they’re saved to ~/.bash_history. The buffer stores up to 1,000 entries, and the history file holds up to 2,000 entries.
🚀 Working with Bash History: The Basics
Bash history helps you view and clear previous commands. Let’s cover the basics.
🔄 How to See Bash History?
To view your bash history, simply run:
history
The output will show a list of commands, with numbers indicating the order they were entered. Use ↑ and ↓ keys to scroll through your history.
Limit the output to the last N commands with:
history N
Filter history output with grep:
history | grep ping
🧹 How to Clear Bash History?
Clear specific entries with:
history -d N
To clear the entire session’s history:
history -c
To clear the saved history file:
cat /dev/null > ~/.bash_history
⚙️ Configuring Bash History Settings with .bashrc
Customize bash history by adding environment variables to your ~/.bashrc file. Apply changes by reloading with:
source ~/.bashrc
🚫 Exclude Commands from Bash History
Prevent specific commands from being saved to history by adding this to ~/.bashrc:
HISTIGNORE='sudo *':'echo w*'
💾 Immediately Persist Commands to .bash_history
To save each command to ~/.bash_history as it’s executed, add:
PROMPT_COMMAND='history -a'
⏰ Add Date and Timestamps to Bash History Output
Include timestamps by adding this to ~/.bashrc:
HISTTIMEFORMAT="%F %T "
📈 Modify Bash History Buffer and File Size
Adjust the size of the history buffer and file with:
HISTSIZE=11000
HISTFILESIZE=11000
⌨️ Search Bash History: Keyboard Shortcuts
Here are some useful keyboard shortcuts for working with bash history:
| Shortcut | Description |
|---|---|
| ↑ | Scroll backwards through history |
| ↓ | Scroll forward through history |
| Ctrl+R | Search history |
| Ctrl+O | Run command found with Ctrl+R |
| Ctrl+G | Exit search |
🔧 Bash History Expansion
Commands like sudo !! make use of history expansion. Here are a few examples:
| Expansion | Description |
|---|---|
!! |
Last command executed |
!N |
Nth command in history |
!-N |
Command N before the most recent |
🛠️ Bash History Designators
Use ! and ^ for powerful history manipulations.
! executes the most recent command matching . ^ replaces parts of the previous command:
^wrongplace^rightplace^
📄 Use Designators to Get Arguments from a Command
Here are some common designators:
| Designator | Description |
|---|---|
!:^ |
First argument of last command |
!:$ |
Last argument of last command |
!:* |
All arguments of last command |
🔨 Bash History Modifiers
Bash history modifiers change the execution of a command:
| Modifier | Description |
|---|---|
:h |
Remove trailing path from a command |
:t |
Remove leading path from a command |
:r |
Remove file extension |
:p |
Print a command without running it |
:s |
String substitution (like sed) |
🔍 Example of Modifiers
Suppose you wanted to change *.txt to *.log:
ls /directory/*.txt
pepperAndEgg.txt
!:s/txt/log
ls /directory/*.log
log1.log log2.log log3.log
🏁 Conclusion
Knowing how to use and customize bash history can save you time and effort at the command line. Use these tips to streamline your workflow and avoid repetitive typing!
