Buy this tutorial as a PDF for only $5
9 Mar 2018

"Again, Again!"
Doing a different thing to the same thing (or, Teletubbies mode, if you prefer!)
Computers are supposed to be good at repetitive tasks, but sometimes we end up repeating things to the computer. For example:
steve@linux:/tmp$ mkdir -p yet/another/directory steve@linux:/tmp$ cd yet/another/directory steve@linux:/tmp/yet/another/directory$ echo "This is yet/another/directory" > README.txt steve@linux:/tmp/yet/another/directory$ cat README.txt This is yet/another/directory steve@linux:/tmp/yet/another/directory$
That's a lot of unnecessary typing. Do we have to keep on saying "yet/another/directory" every time we refer to it?
No, we don't, and once you get into the habit of using $_
, I promise you'll never look back.
steve@linux:/tmp$ mkdir -p yet/another/directory steve@linux:/tmp$ cd $_ steve@linux:/tmp/yet/another/directory$ echo "This is $_" > README.txt steve@linux:/tmp/yet/another/directory$ cat README.txt This is yet/another/directory steve@linux:/tmp/yet/another/directory$
We only had to type the long "yet/another/directory" text one time. $_
substituted for it all the rest of the time.
How?
Because $_
inserts the final argument of the previous command. And it keeps on going, so we can keep using $_
for "the last argument of the previous command" as many times as we like.
Other Examples: Files
Many *nix commands take a filename as the final argument, so some obvious examples are to do with filenames.
steve@linux:/tmp$ echo foo >> myfile.txt steve@linux:/tmp$ chmod 755 $_ steve@linux:/tmp$
Or you might want to copy a file after editing it:
steve@linux:/tmp$ vi myfile.txt steve@linux:/tmp$ scp $_ 192.168.3.27: myfile.txt 100% 14 0.0KB/s 00:00 steve@linux:/tmp$
Note that at this point, we have broken the spell; $_
is now 192.168.3.27:
and not myfile.txt
. This can be used to your advantage, it just depends on what you are trying to achieve.
More Examples: Git
Git repositories tend to be in a directory of the same name. This one is a little more involved, but follows the same principle. We want to clone a repo from the URL https://github.com/githubtraining/hellogitworld.git. This will create a "hellogitworld" directory. The basename
utility will strip out the prefix, leaving "hellogitworld.git", and using the second ".git" argument, will also trim ".git" from the end. This converts "https://github.com/githubtraining/hellogitworld.git" into "hellogitworld", and we can cd hellogitworld
without retyping anything:
steve@linux:/tmp$ git clone https://github.com/githubtraining/hellogitworld.git Cloning into 'hellogitworld'... remote: Counting objects: 306, done. remote: Total 306 (delta 0), reused 0 (delta 0), pack-reused 306 Receiving objects: 100% (306/306), 95.31 KiB | 0 bytes/s, done. Resolving deltas: 100% (71/71), done. Checking connectivity... done. steve@linux:/tmp$ cd `basename $_ .git` steve@linux:/tmp/hellogitworld$ ls build.gradle fix.txt pom.xml README.txt resources runme.sh src steve@linux:/tmp/hellogitworld$
Summary
Once you get into the habit of using $_
, you start finding more and more ways of using it. Give it a try for a few days, and see if you don't get hooked!
Appreciate this site? Please consider making a donation:
Books and eBooks
My Shell Scripting books, available in Paperback and eBook formats.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and to keep by your desk as an ever-present companion. | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half explains the features of the shell; the second half has real-world shell scripts, organised by topic, with detailed discussion of each script. |
Contact
You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!