The Shell Scripting Tutorial


Again, Again!

Doing a different thing to the same thing (or, Teletubbies mode, if you prefer!)

9 Mary 2018 (updated 5th Aug 2021)

Again, Again!

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$ 

Caveat

The only that I have found potentially confusing, is that the "$_" itself gets reset each time, so here, we get in the habit of assuming that "$_" will always mean "world".

We can repeat "echo $_" and it still says "world" every time. But once we have added "is round" to the previous command, the value of "$_" changes to be "round", so this command is not repeatable, even though the previous one did consistently replace "$_" with "world" each time:

$ echo hello
hello
$ echo world
world
$ echo $_
world
$ echo $_
world
$ echo $_ is round
world is round
$ echo $_ is round
round is round
$ 

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!


My Paperbacks and eBooks

My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.

Shell Scripting Tutorial

Shell Scripting Tutorial
is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion.

Also available in PDF form from Gumroad:Get this tutorial as a PDF
Shell Scripting: Expert Recipes for Linux, Bash and more

Shell Scripting: Expert Recipes for Linux, Bash and more
is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script.