7 Bash History Shortcuts to Quickly Fix Mistyped Commands
This guide introduces seven Bash history‑expansion shortcuts—such as !$, !:n, !:1-$, !-n:$, !$:h, !#:1, and !!:gs—that let Linux users retrieve or modify parts of previous commands, dramatically reducing the effort required to correct long or complex command lines.
1. Retrieve the last argument with !$
The !$ token expands to the last argument of the previous command. For example, after a failed mv /path/to/错误的文件 /some/other/place, you can reuse the correct file name without retyping it:
$ mv /path/to/对的文件 !$
mv /path/to/对的文件 /some/other/place2. Access the n‑th argument using !:n
Position parameters can be referenced with !:0 (the command itself), !:1 (first option), etc. This lets you reorder arguments without re‑entering long strings. Example:
$ !:0 !:1 !:3 !:2
tar -cvf afolder.tar afolder3. Get all arguments with !:1-$
!:1-$expands to every argument after the command name. It is handy when you typed the wrong command but want to keep the same arguments, e.g. converting a mistaken zip invocation to tar:
$ tar !:1-$
# expands to: tar -cvf afolder.tar afolderYou can also select a subset, such as !:1-2 or !:3-9, when only part of the argument list is needed.
4. Retrieve arguments from the n‑th previous command with !-n:$
The !-n reference points to the command entered n lines ago. Combined with :$, it returns the last argument of that command. Example:
$ mv /path/to/rightfile !-2:$
# expands to the path from two commands earlier5. Extract the directory part of the last argument using !$:h
When a command fails because a file does not exist, !$:h gives the directory component of the last argument, allowing you to cd directly there:
$ cd !$:h
# becomes: cd /etc6. Fetch the first element of the current line with !#:1
For commands where the first argument repeats (e.g., copying a file and then creating a backup), !#:1 inserts that first argument, saving keystrokes:
$ cp /path/to/file !#:1.bak
# expands to: cp /path/to/file /path/to/file.bak7. Substitute a string in the previous command using !!:gs/old/new/
The !!:gs modifier performs a global substitution on the most recent command. To replace the typo f with s:
$ !!:gs/f/s/
# becomes: echo my s key does not workYou can chain substitutions, e.g., !!:gs/does/did/, to correct multiple parts at once.
Conclusion
By mastering these seven Bash history‑expansion shortcuts, Linux users can correct long or complex commands with a few keystrokes, dramatically improving command‑line productivity.
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.
