Operations 10 min read

8 Surprising Ways to Use the ‘!’ Operator in Linux Commands

This article explores eight practical uses of the ‘!’ history‑expansion operator in Linux shells, covering numbered command recall, negative indexing, argument reuse, multi‑parameter handling, keyword‑based recall, repeat‑last‑command shortcuts, selective file deletion, and directory‑existence checks, each illustrated with concrete examples.

Linux Tech Enthusiast
Linux Tech Enthusiast
Linux Tech Enthusiast
8 Surprising Ways to Use the ‘!’ Operator in Linux Commands

1. Run a command by its history number

After listing the command history with history, you can execute a previous command by prefixing its line number with !, e.g., !58 runs the command on line 58 of the history.

linuxmi@linuxmi:~/www.linuxmi.com$ history
$ !58

The exact number varies per session.

2. Execute a previously run command using negative indexing

The !-n syntax refers to the nth most recent command, where -1 is the last command, -2 the one before, and so on.

$ history</code><code>$ !-3</code><code>$ !-6</code><code>$ !-10

3. Pass the previous command’s arguments to a new command

Use !$ to reuse the last argument of the preceding command. For example, after ls /home/linuxmi/snap, you can run ls -l !$ to list the same directory in long format.

$ ls /home/linuxmi/snap
$ ls -l !$

4. Work with two or more parameters

When a command has multiple arguments, !^ expands to the first argument, !cmd:2 expands to the second argument of cmd, and !* expands to all arguments.

linuxmi@linuxmi ~/www.linuxmi.com% touch /home/linuxmi/linuxmi.go
linuxmi@linuxmi ~/www.linuxmi.com% cp /home/linuxmi/linuxmi.go /home/linuxmi/go
% echo "First argument is: !^"
First argument is: /home/linuxmi/linuxmi.go
% echo "Second argument is: !cp:2"
Second argument is: /home/linuxmi/go

5. Run the most recent command matching a keyword

Prefix a command with ! followed by a keyword to repeat the last command that started with that keyword.

$ ls /home > /dev/null      [Command 1]</code><code>$ ls -l /home/linuxmi/linuxmi > /dev/null   [Command 2]</code><code>$ ls -la /home/linuxmi/linuxmi.com > /dev/null   [Command 3]</code><code>$ ls -lA /usr/bin > /dev/null   [Command 4]
$ ! ls      # repeats Command 1</code><code>$ ! ls -l   # repeats Command 2</code><code>$ ! ls -la  # repeats Command 3</code><code>$ ! ls -lA  # repeats Command 4

6. Repeat the last command with !!

The double‑bang shortcut expands to the previous command. It can be combined with su -c "!!" root to re‑execute the last command as root.

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

7. Delete all files except a specific one

Using the extended glob !(pattern) with rm removes everything that does not match the pattern.

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

8. Check whether a directory exists

Combine the test [ ! -d /path ] with logical AND ( &&) and OR ( ||) to print messages or take actions based on the directory’s presence.

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

The article concludes by inviting readers to share any additional “!” tricks they know.

Command Linebashshell scriptinghistory expansionbang operator
Linux Tech Enthusiast
Written by

Linux Tech Enthusiast

Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.

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.