Master Bash History: Powerful ‘!’ Expansion Tricks for Faster Command Reuse
This guide explains how Bash stores command history, the role of HISTSIZE and HISTFILE, and demonstrates practical uses of the ‘!’ expansion—including pattern matching, repeating specific or recent commands, combining with sudo, piping, and on‑the‑fly string substitution—to dramatically speed up command‑line workflows.
Bash keeps a record of every command you type in its history list, whose size is controlled by the HISTSIZE variable (default 500) and whose file location is defined by HISTFILE (default ~/.bash_history).
Basic "!" Expansion
The ! operator pulls commands from the history list into the current input line, allowing you to quickly repeat, edit, or reuse previous commands without retyping them.
Repeating Commands by Prefix
!l– repeats the most recent command that starts with l (e.g., ls /root/). !ls – repeats the last command that begins with ls.
Repeating Any Matching Command
!?followed by a string searches the entire history for the most recent command containing that string, regardless of its position. Example:
[root@localhost ~]# cat employee.json
[{"name": "John Brooks", "id": "003"}, ...]
[root@localhost ~]# !?employee
cat employee.json
[...same output...]Repeating the Nth Command
Use !<em>n</em> where n is the command number in the history. Example: !772 re‑executes command number 772.
Repeating the Last Command
!!– repeats the most recent command (equivalent to !-1).
Combine with sudo: sudo !! re‑runs the previous command with root privileges.
Pipe the result: !! | grep file runs the last command and filters its output.
String Substitution in the Last Command
Use the syntax !!:s^oldstring^newstring to replace the first occurrence of oldstring with newstring in the previous command. The caret ( ^) acts as the delimiter. !!:s^conf.d^conf After running ll /etc/httpd/conf.d, the above substitution changes the command to ll /etc/httpd/conf.
Practical Examples
# List files in /root and repeat with prefix
[root@localhost ~]# ls /root/
[root@localhost ~]# !l
ls /root/ # Use sudo with the previous command
$ yum update
$ sudo !!
sudo yum update # Pipe the last command's output
$ ls
file file1 file2
$ !! | grep file
file
file1
file2Conclusion
Understanding and leveraging Bash’s history and ! expansion can save system administrators and developers considerable time, reducing repetitive typing, correcting mistakes on the fly, and streamlining complex command sequences.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
