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.
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 !582. 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 !-63. 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 !$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"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 -lA6. 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.txtWhen a command must be re‑executed with elevated privileges, !! can be embedded in su -c or sudo:
su -c "!!" root7. 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.comThese history‑expansion features are native to Bash; other shells may implement them differently or not at all.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
