Bash: Using '$_' and '$1' to Manage Repetitive Command Line Tasks

A few examples of how you can recycle arguments in Bash commands using the '$_' and '$1' shorthand aliases. This is a great way to save time and keystrokes when working in Bash

Background: Just Too Many Keystrokes

The more time you spend on the command line, the more you realize much of your time there is occupied by typing out the same thing (i.e., a filename, or certain command) over and over. Bash shines in its ability to take long complicated commands and store them in aliases. No matter how many aliases you create, though, you'll still need to dedicate a little brain power to your command line surfing. Sometimes, aliases just don't apply.

Let's look at a quick example--repetitive keystrokes you're bound to run into at some point in your workflow: let's say I have I file I need to rename and then edit. That might look something like this (I've highlighted redundant keystrokes in red):

# renaming a conf file 
terracoders@tc:/etc/apache2/sites-enabled$ sudo mv some-ridiculously-long-site-nmae.conf some-ridiculously-long-site-name.conf 
# opening the same file for editing, now that the name is fixed 
terracoders@tc:/etc/apache2/sites-enabled$ sudo nano some-ridiculously-long-site-name.conf 

The most burdensome part of this is that the file name I'm working with is long and complicated. There's really not a whole lot I can do about the first command (the tab key can help autocomplete the file names--but it's not 100% if there are multiple files with similar naming schemes); most people are going to groan their way through the second command as well. This second of these commands is where the shorthand $_ can help fill in the gaps.

Usage: Applied Examples of '$_'

$_ is a Bash shorthand referencing the final argument in whatever your last command was. So let's have a look at the example from above once more. That second command can be shortened down like this--again, using red to illustrate elements with the same value:

# renaming a conf file 
terracoders@tc:/etc/apache2/sites-enabled$ sudo mv some-ridiculously-long-site-nmae.conf some-ridiculously-long-site-name.conf 
# opening the same file for editing, now that the name is fixed 
terracoders@tc:/etc/apache2/sites-enabled$ sudo nano $_ 

So long as I don't offer any new final arguments, I can keep going with $_ as shorthand. Let's say that I now need to copy the file some-ridiculously-long-site-name.conf into another directory; I can even inject the $_ shorthand into the destination file path. Check it out:

# copying the same file to another directory 
terracoders@tc:/etc/apache2/sites-enabled$ sudo cp $_ ../sites-available/$_  

Pretty cool, right? Give it a try! It may take a little getting used to; you need to make a mental note at first, identifying when exactly you can use it. If you make a habit out of using it, though, you'll quickly find that it's just one more of those Bash tricks you can't do without!

Bonus Round: Using $1

If you've played with Bash scripting at all, you'll probably be familiar with the $1 variable. This variable generally represents the first argument in a command. Most often, I use this variable to pass an argument to a script. It has other applications, though!--here's another really nifty trick! You can use the $1 variable to reuse an argument already defined in the same command. Let's say you have a file you need to move to a new directory. Assuming you're already in the file's directory, you'd typically move the file like this: $ mv file.txt /new/path/to/file.txt Here, the first argument in our command is file.txt; we can use the $1 variable to capture that argument and place the file name within it's new path:

# moving a file to another directory 
terracoders@tc:~$ mv file.txt new/path/to/$1 

Using $1 as a variable won't stop you from using $_ , either; after moving our file in the example above, let's say we want to open the file and edit it from it's new location. That would look like this:

# moving a file to another directory 
terracoders@tc:~$ mv file.txt new/path/to/$1

# edit the file from its new location 
terracoders@tc:~$ nano $_

A Penny Saved

Reusing arguments is awesome! Will it put an end to global warming?--probably not. But, shaving a minute here and there from your work-flow is something to celebrate. Those minutes can add up! Again, neither of these techniques will come naturally when you first start to use them. If you commit to using them, though, I promise you'll get used to them quickly!