This post was originally published on go2linux.org on January 4, 2009. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

Introduction

The Bash history command lists your recent commands. Once you have found the one you need, there is no reason to retype it — Bash provides several fast ways to bring it back and execute it.

For how the history command itself works (listing, searching, configuring history size), see history — the Linux command to manage your command line history.

Re-run a command by number — !N

Type ! followed immediately by the command number from history output (no space):

history
  466  man nohup
  467  sudo apt install vim
  468  sudo vim /etc/apt/sources.list
  469  sudo apt update
  470  man nice

To re-run command 470:

!470

This executes man nice immediately. There is no confirmation prompt.

Re-run the last command — !!

!! expands to the full previous command:

apt update
# bash: permission denied

sudo !!
# Runs: sudo apt update

This is most useful when you forget sudo.

Re-run the last command starting with a string — !string

!string searches backward through history for the most recent command that starts with string:

!vim          # re-runs the last command that started with "vim"
!sudo apt     # re-runs the last command that started with "sudo apt"

Use part of the last command — !$, !^, !*

These event designators let you reuse parts of the last command.

Shortcut Expands to
!$ Last argument of the previous command
!^ First argument of the previous command
!* All arguments of the previous command

Example — !$:

mkdir /var/www/myapp
cd !$        # cd /var/www/myapp

Example — !^:

diff file1.txt file2.txt
vim !^       # vim file1.txt

Quick substitution — ^old^new

Replace a word in the previous command and run the corrected version:

cat /etc/ngnix/nginx.conf
# typo in the path

^ngnix^nginx
# Runs: cat /etc/nginx/nginx.conf

Reverse search — Ctrl+R

Press Ctrl+R and start typing. Bash searches backward through history and shows the most recent match as you type.

(reverse-i-search)`vim': sudo vim /etc/nginx/nginx.conf
  • Press Ctrl+R again to jump to the previous match
  • Press Enter to run the shown command
  • Press Ctrl+G or Escape to cancel without running anything

Ctrl+R is often the fastest way to re-run a command you ran recently but do not remember the number of.

Preview before running — :p

To see what !N or !string would expand to without actually running it, append :p:

!470:p        # prints the command but does not run it
!vim:p        # prints the matching command but does not run it

The command is also added to history so you can press Up to edit it before running.

Summary

Shortcut What it does
!N Re-run command number N
!! Re-run the last command
!string Re-run the last command starting with string
!$ Last argument of the previous command
!^ First argument of the previous command
!* All arguments of the previous command
^old^new Replace old with new in the last command
Ctrl+R Interactive reverse search through history
!N:p Print command N without running it

See also