🧠 What is history?
history shows a list of commands you’ve run in the shell (current session + what was read from ~/.bash_history on startup).
🔧 Common history Commands and Options
🔹 history
Displays the full list of past commands with line numbers.
$ history
1 ls
2 cd /etc
3 cat resolv.conf
4 sudo poweroff🔹 history -w
Writes the current session’s in-memory history to the ~/.bash_history file manually.
history -w✅ Use this if you want to make sure all commands are saved before exiting or powering off.
🔹 history -c
Clears the current session’s in-memory history.
history -c⚠️ This doesn’t delete ~/.bash_history immediately. To fully erase history:
history -c && history -wThis clears the session and writes an empty history file.
🔹 history -a
Appends new session commands to the history file immediately (normally done at shell exit).
history -a🧠 Useful if you’re scripting or want history saved after every command (you can automate it with PROMPT_COMMAND='history -a').
🔹 history --help
Displays help for the history built-in (because it’s a Bash built-in, not a standalone binary).
history --helpNote: You can also use:
help history⚡ Special History Shortcuts
These let you quickly reuse previous commands.
🔹 !! – Repeat Last Command
!!Runs the last command again.
Example:
$ echo "Hello"
Hello
$ !!
echo "Hello"
Hello🔹 !n – Run Command Number n
!5Runs the 5th command from history.
You can find the number using:
history📝 Quick Summary Table
| Command | What It Does |
|---|---|
history | Show history list |
history -w | Write in-memory history to file |
history -c | Clear in-memory history |
history -a | Append current session to file |
history --help | Show built-in help for history |
!! | Run the last command again |
!n | Run command number n from history |