Quick take: The history command lists the commands you have run. Rerun one with !number, repeat the last command with !!, and search interactively with Ctrl+R. Clear it with history -c.

Introduction

The shell remembers the commands you type, and the history command lets you review and reuse them — saving you from retyping long commands and helping you recall exactly what you did earlier. Combined with a few keyboard shortcuts, it makes the command line far faster.

This guide covers viewing history, rerunning commands by number, reverse-searching, and managing what bash stores.

Syntax

The basic syntax of the history command is:

history [OPTIONS]

Common Options and Parameters

The most useful options and parameters for the history command:

OptionDescription
historyList the command history with line numbers.
history NShow the last N commands.
history -cClear the current session's history.
!NRun the command with history number N.
!!Run the previous command again.
!stringRun the most recent command starting with 'string'.
Ctrl+RReverse-search history interactively as you type.
HISTSIZE / HISTFILESIZEControl how many commands bash keeps.

Practical Examples

Real history commands you can run today:

# Show the full command history
history
# Show the last 20 commands
history 20
# Rerun command number 512
!512
# Repeat the previous command (often with sudo)
sudo !!
# Rerun the last apt command
!apt
# Search history interactively (type, then Enter)
Ctrl+R

Configuring and Securing Bash History

A few settings in ~/.bashrc make history dramatically more useful. They control how much is kept, whether duplicates are stored, and whether timestamps are recorded.

# ~/.bashrc
HISTSIZE=10000          # commands kept in memory
HISTFILESIZE=20000      # commands kept in the history file
HISTCONTROL=ignoreboth  # ignore duplicates and leading-space commands
HISTTIMEFORMAT='%F %T ' # record a timestamp with each command
shopt -s histappend     # append rather than overwrite on exit

The ignoreboth setting has a handy side effect: any command you start with a leading space is not saved at all — useful for keeping a one-off command containing a password or token out of your history.

Tips and Best Practices

  • sudo !! reruns your last command with sudo — perfect after a “permission denied”.
  • Press Ctrl+R and start typing to fuzzy-search your history; press it again to cycle older matches.
  • Add HISTTIMEFORMAT='%F %T ' to your ~/.bashrc to record timestamps with each command.

Final Thoughts

The history command and its shortcuts turn the shell into a fast, searchable record of your work. Learn !!, !N, and especially Ctrl+R, and you will rarely retype a long command again. Tune HISTSIZE and timestamps to make your history even more useful for auditing and recall.

FAQ: history Command in Linux

How do I see my command history in Linux?+

Run history to list all remembered commands with numbers, or history 20 to see just the last 20. Bash stores history in ~/.bash_history.

How do I rerun a previous command?+

Use !N where N is the history number (e.g. !512), !! to repeat the very last command, or !string to rerun the most recent command starting with that text.

What does sudo !! do?+

It reruns your previous command with sudo. It is the classic fix after a command fails with 'permission denied' — just type sudo !! and press Enter.

How do I search my command history?+

Press Ctrl+R and start typing; bash shows the most recent matching command. Press Ctrl+R again to step back through older matches, then Enter to run it.

How do I clear my command history?+

Use history -c to clear the current session, and delete ~/.bash_history (or run history -w after clearing) to remove the saved file.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support