history
Clear history list
Delete all entries from the current shell’s history list (memory only):
history -c
Delete specific history entries
Delete a single entry by position
history -d OFFSET
Example (delete entry number 150)
history -d 150
Negative offsets count backward from the end
history -d -1
Deletes the most recent command
history -d $(history 1 | awk '{print $1}')
Delete a range of entries
history -d START-END
- Example:
Deletes entries 100 through 120 (inclusive).
history -d 100-120
Write and read history files
Append current session to history file
Writes only new commands from this session to ~/.bash_history:
history -a
Read new history lines from file
Reads commands added by other shells:
history -n
Read entire history file
Appends the full history file into the current session:
history -r
Write current history list to file
Overwrites the history file with the current in-memory list:
history -w
History expansion without saving
Expand history references (!!, !$, etc.) without storing the result
history -p ARG
History Expansion: Print Last Command Without Executing
history -p !!
Displays the expanded command only.
Manually add commands to history
Append arguments as one single history entry
history -s COMMAND
Example:
history -s "ssh user@host -p 2222"
- Useful for logging commands you ran elsewhere.
Common operational patterns
Sync history across terminals (manual)
history -a
history -n
Remove last command from history
history -d -1
Remove specifik line from history, in this example we remove line 1093 from history
history -d 1093
Clear history and write immediately
history -c
history -w
List of commands you use most often
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
Notes
history -cclears memory only until followed byhistory -whistory -ais append-safehistory -woverwrites the history file- Range deletion is permanent once written