Master Linux History Expansion: Powerful ‘!’ Operator Tricks
This guide explains the versatile Linux ‘!’ history‑expansion operator, showing how to repeat previous commands, substitute arguments, use wildcards, edit past entries, and apply safety tips, all with clear examples to boost command‑line efficiency.
History Command Lookup and Execution
The ! operator lets you search and run commands from your shell history, saving time and keystrokes.
1. Repeat the last command
Use !! to execute the most recent command again:
!!2. Repeat a command that starts with a keyword
Enter !keyword to run the last command whose first word matches the keyword, e.g.: !ls This runs the most recent command beginning with ls.
3. Run a specific command by its history number
If you know the command’s index in the history list, use !number:
!1234. Use wildcards with history expansion
Combine ! with a wildcard to match patterns, such as executing the last git command:
!git*Command‑Line Argument Substitution
The ! operator can also replace arguments from previous commands.
1. Last argument of the previous command
Use !$ to retrieve the final argument:
ls /path/to/some/directory
cd !$2. Specific argument by position
Use !:n where n is the 1‑based index of the desired argument:
ls file1 file2 file3
cat !:2This runs cat file2.
Advanced Usage: Editing and Executing History Commands
Combine ! with other features for more powerful workflows.
1. Edit and run the last matching command
Enter !?keyword to search for a command containing the keyword, edit it, and then execute:
!?search2. Edit and re‑execute the previous command
Use !!:e to open the last command in an editor, modify if needed, and run it again:
ls /path/to/some/directory
!!:ePractical Examples
Below are common scenarios that illustrate how ! can speed up everyday tasks.
1. Repeating similar commands
grep keyword1 /path/to/file
grep keyword2 /path/to/file
grep keyword3 /path/to/fileWith history expansion:
!grep keyword1 !$
!grep keyword2 !$
!grep keyword3 !$2. Quickly switch back to the previous directory
cd /very/long/path/to/directory
cd !$3. Find and edit a previous command
!?search_termThis opens the most recent command containing search_term for editing before execution.
Precautions When Using !
Ensure your command history is intact; use history to view and manage it.
Verify keywords and argument indices are correct to avoid unexpected results.
Avoid storing sensitive data (e.g., passwords) in the history.
When editing commands, double‑check the final command before running.
Conclusion
The ! operator is a powerful tool for Linux users, enabling rapid reuse of past commands, argument substitution, and advanced editing. Understanding its various forms and safety considerations helps make the command line more efficient and less error‑prone.
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.
