How to View and Reuse Past Commands in Linux (history)

The history command lists previously executed shell commands, and integrates with shortcuts for quickly reusing them.

Published October 1, 2026

history lists commands you ran earlier in the shell. Reuse them with !!, !n, !string or the Ctrl+R reverse search.

How it works

Bash keeps history in memory and writes it to ~/.bash_history on exit. HISTSIZE and HISTCONTROL tune behaviour.

Never type secrets in commands, since they get recorded.

Try it

history | tail -3
101 git pull
102 npm test
103 history | tail -3

Practical examples

Rerun the last command with sudo

sudo !!

Run by number

!102

Search history

history | grep ssh
# or press Ctrl+R and type

Ignore duplicates and spaces

export HISTCONTROL=ignoreboth

Common mistakes

  • Storing passwords on the command line.
  • Expecting history to save when the terminal crashes.
  • Running !n blindly.

Key takeaways

  • history, !!, Ctrl+R.
  • HISTCONTROL=ignoreboth hides space-prefixed commands.
  • Keep secrets out of commands.

FAQ

How do I clear history?

history -c and delete ~/.bash_history.

Share history across tabs?

history -a; history -n in PROMPT_COMMAND.

More Linux articles