history
what did I typehistory prints your recent commands with a number beside each. Any of them can be run again by number, and Ctrl+R searches backwards through them as you type — the fastest way to find a long command from last week.
Ctrl+R, then a few letters, finds a command you ran before. Enter runs it.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
The most recent are at the bottom. The number is what you use to refer to one.
!503 runs command 503 again. The shell prints the command it expanded to before running it.
Forgot sudo? "sudo !!" repeats the last command with sudo in front.
Switches
A switch changes how the command behaves. These are the ones used day to day; man history lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
NUM- Show only the last NUM commands: history 20.
-c- Clear the history in this shell.
-d NUM- Delete entry number NUM.
-w- Write the current history to the history file now.
-a- Append this session’s new lines to the history file.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
history | grep sshFinds every earlier command that mentioned ssh.
history -cClears the history held in this shell’s memory.
history -d 503Deletes a single entry by number.
export TOKEN=abc123A leading space keeps a command out of history, where HISTCONTROL is set to ignorespace.
In cybersecurity
Your history is a plain-text record of everything you typed, passwords included.
A password given as an argument, or a token in an export, is saved to ~/.bash_history in the clear. It is one of the first files read on a compromised account.
History is written by the user’s own shell and can be edited or switched off. For real accountability use auditd or sudo’s logging.
An empty history file, or one linked to /dev/null, on an account that is used daily suggests someone tidied up.