Bash !$ Trick: Insert the Last Argument of the Previous Command

Command-line efficiency is one of the cornerstones of a productive Linux or Unix workflow. Whether you’re administering a system, writing scripts, or simply navigating files, knowing a few powerful shortcuts can make your experience smoother and significantly faster. One such time-saving feature in Bash and other popular shells is the !$ trick.

This shortcut allows you to recall and reuse the last argument from the previous command without having to retype it. Especially when dealing with lengthy file paths or complex commands, !$ can save time and reduce the chance of errors. In this guide, we’ll explore how it works, look at examples, and compare it with related tools like $_ and the Alt + . keyboard shortcut. If you’re just starting to explore shell scripting, it might be useful to first read this beginner-friendly guide to echo in shell scripting.

The !$ shortcut is a powerful feature available in Bash, csh, tcsh, and zsh shells. It allows users to quickly reuse the last argument of the previous command without retyping it. This trick significantly speeds up command-line operations, especially when working with long paths or filenames.

This feature is not available in ksh (KornShell), which does not support the same history expansion syntax. Bash and the other supported shells interpret !$ as a reference to the last word from the preceding command.

While it might seem like a small convenience, the ability to automate argument reuse becomes extremely useful when you’re performing multiple operations on the same file or directory. It’s an ideal addition to your command-line toolkit.

Basic Example in Bash

Let’s look at a simple example. Suppose you run the command ls -l /etc/passwd to list the detailed information about the file. If you then want to use another command, such as cat, on that same file, you can avoid retyping the full path by using !$.

$ ls -l /etc/passwd
-rw-r--r-- 1 root root 2579 Jul 24 23:52 /etc/passwd

$ cat !$
cat /etc/passwd
-rw-r--r-- 1 root root 2579 Jul 24 23:52 /etc/passwd

This works by Bash expanding !$ to match the last argument (/etc/passwd) from the previous command. It’s important to note that the shell shows you the expanded command before executing it, giving you the chance to see what will be run.

This behavior is consistent across many commands. Whether you’re using ls, cat, mv, or any other tool, !$ will always refer to the last argument used previously, making it a reliable and predictable shortcut.

Another Example

To further illustrate the use of !$, let’s say you extract an archive file and immediately want to move it to a different location. Instead of typing the archive name twice, you can reuse the argument with !$.

$ tar -xzf archive.tar.gz
$ mv !$ /tmp
mv archive.tar.gz /tmp

Here, archive.tar.gz is the last argument from the tar command, and it is automatically inserted into the mv command. This avoids duplication and streamlines repetitive tasks.

This technique becomes even more powerful in scripts or command sequences where reusing arguments is frequent. It’s a great way to maintain momentum in the terminal without slowing down to copy and paste or retype filenames.

Additionally, when managing file output redirection, it pairs well with techniques like those outlined in this guide on redirecting stdout and stderr to multiple files.

Alternative: The $_ Variable

In addition to !$, Bash provides the special variable $_, which also holds the last argument of the previous command. This variable can be used in scripts or interactively in the same way.

$ ls -l /etc/shadow
-rw-r----- 1 root shadow 2559 Jul 24 23:52 /etc/shadow

$ cat $_
cat /etc/shadow

Although the functionality is similar, $_ behaves slightly differently. It persists as a shell variable and can be useful in contexts where history expansion like !$ might not be ideal, such as in scripts or with certain quoting scenarios.

Knowing both !$ and $_ gives you flexibility. You can choose the one that best fits the context you’re working in, whether it’s a one-liner in the terminal or a more complex script.

Keyboard Shortcut: Alt + .

There’s also a convenient keyboard shortcut in Bash for inserting the last argument: Alt + .. This key combination (press Alt and the period key) does the same thing as !$ but is especially helpful in interactive use.

$ echo /var/log/syslog
$ cp [Alt + .] /backup
cp /var/log/syslog /backup

Using Alt + . allows you to insert the last argument without typing a special syntax. You can even press it multiple times to cycle through previous arguments, depending on your shell configuration.

This shortcut becomes second nature with regular use, and it works well with other command-line editing features in Bash, like Ctrl + A (move to start) or Ctrl + E (move to end). It’s a seamless way to speed up repetitive command patterns.

Why Learn This?

Mastering !$, $_, and Alt + . can greatly improve your productivity in the terminal. They help reduce keystrokes, speed up repetitive tasks, and avoid typos when dealing with complex paths or filenames.

Even seasoned users sometimes overlook these shortcuts, but once you incorporate them into your routine, you’ll wonder how you managed without them. They are particularly useful in environments where precision and speed are important—like server maintenance or development workflows.

Whether you’re a beginner or a power user, understanding and using these tools will enhance your command-line experience. Combine them with other Bash features like !!, command substitution, and aliases to make your shell sessions faster and more efficient.

Conclusion

The !$ trick, along with its counterparts $_ and Alt + ., are small but powerful features that can make a big difference in your command-line productivity. They reduce the need to retype or copy arguments and streamline repetitive command patterns, especially in file management or scripting workflows.

By incorporating these shortcuts into your daily usage, you not only save time but also gain a deeper understanding of how Bash handles command history and arguments. These tricks are easy to learn and offer immediate benefits, making them essential for anyone serious about mastering the shell.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *