Fundamentals 10 min read

Master Linux History Expansion: 8 Powerful ‘!’ Command Tricks

This guide explains eight practical uses of the ‘!’ operator in Linux shells, covering how to rerun commands by number, reference previous arguments, handle multiple parameters, search by keyword, repeat the last command, and perform conditional deletions and directory checks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux History Expansion: 8 Powerful ‘!’ Command Tricks

1. Run a command by its history number

List the command history with history. Each line is prefixed with a number. To re‑execute a specific entry, type !N where N is the line number.

history
!58
Running a command by its history number
Running a command by its history number

2. Run a recent command by relative offset

The !-n syntax refers to the command n lines back from the current prompt. !-1 repeats the last command, !-2 the one before that, and so on.

!-3
!-6
Re‑running a command by relative offset
Re‑running a command by relative offset

3. Reuse the last argument of the previous command

!$

expands to the final argument of the most recent command. This is handy when you need to feed the same path or filename into a different command.

ls /home/linuxmi/snap
ls -l !$
Using !$ to reuse the last argument
Using !$ to reuse the last argument

4. Access specific arguments of a previous command

Several shortcuts exist for extracting arguments from the most recent command: !^ – first argument !$ – last argument !cmd:n – the n ‑th argument of the most recent command that started with

cmd
!*

– all arguments

Example with cp:

cp /home/linuxmi/linuxmi.go /home/linuxmi/go
echo "first argument: !^"
echo "second argument: !cp:2"
Handling multiple arguments
Handling multiple arguments

5. Run the most recent command that matches a pattern

Prefix ! with a string to search the history for the latest command that begins with that string. The matched command is executed as‑is.

!ls
!ls -l
!ls -la
!ls -lA
Running command by keyword
Running command by keyword

6. Repeat the previous command with !!

!!

expands to the entire previous command line. It can be combined with redirection, pipes, or other shell constructs.

ip addr show | grep inet | grep -v 'inet6' | grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d/
!! > ip.txt

When a command must be re‑executed with elevated privileges, !! can be embedded in su -c or sudo:

su -c "!!" root

7. Exclude files from globbing with extended pattern matching

Enable Bash extended globbing ( shopt -s extglob) and use !(pattern) to match everything except the given pattern.

rm !(important_file.txt)
rm !(*.pdf)

8. Test for directory existence

Use the test operator [ ! -d DIR ] together with logical AND ( &&) or OR ( ||) to perform actions based on the presence of a directory.

[ ! -d /home/linuxmi/linuxmi.com ] && printf '
no such /home/linuxmi/linuxmi.com directory
' || printf '
/home/linuxmi/linuxmi.com directory exists
'
[ ! -d /home/linuxmi/linuxmi.com ] && exit
[ ! -d /home/linuxmi/linuxmi.com ] && mkdir /home/linuxmi/linuxmi.com

These history‑expansion features are native to Bash; other shells may implement them differently or not at all.

Linuxshellterminalcommand historybang operator
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.