Beginner · 5 min · practise every command in the built-in terminal

Command History

Bash remembers your previous commands.

View history

history              # All history
history 20           # Last 20 commands

History shortcuts

ShortcutAction
↑ / ↓Previous / next command
Ctrl+RReverse search — type to search history
!!Repeat last command
!nRun command number n
!textRun last command starting with text
!$Last argument of previous command
Ctrl+PPrevious command (same as ↑)

Search history

history | grep ssh
history | grep apt

Clear history

history -c              # Clear current session history
history -d 100          # Delete entry 100
rm ~/.bash_history      # Delete history file

History file

cat ~/.bash_history

Useful settings in .bashrc

export HISTSIZE=10000           # In-memory history
export HISTFILESIZE=20000       # On-disk history
export HISTCONTROL=ignoredups   # No duplicate entries
export HISTTIMEFORMAT="%F %T "  # Add timestamps

Practice tasks

  1. View your command history
  2. Search history for 'ls' commands
  3. Repeat your last command using the history shortcut
  4. Show only the last 10 history entries

Commands in this lesson

Shell & Environment