🧠 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 -w

This 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 --help

Note: 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

!5

Runs the 5th command from history.

You can find the number using:

history

📝 Quick Summary Table

CommandWhat It Does
historyShow history list
history -wWrite in-memory history to file
history -cClear in-memory history
history -aAppend current session to file
history --helpShow built-in help for history
!!Run the last command again
!nRun command number n from history

Obj 103.1 Command line history