COMP2041/COMP9041 – Quoting in Shell Scripts

COMP2041/COMP9041 – Quoting in Shell Scripts

  11 Sep 2019   , ,

[MUSIC PLAYING] In this video,
we’re going to talk about quoting in shell script. You will have used quoting in
other programming languages. In C, you would have
used double quotes for strings and single
quotes for characters. In Python, it wouldn’t have
mattered whether or not you used single
or double quotes. In shell script, there are
three types of quoting. We’ve got single quotes,
which are used for strong quoting. We’ve got double quotes,
which are used for weak quoting. And we’ve got the type
of quote you might not have seen before– back-quotes, which are used
for command substitution. So, first up,
we’re going to have a look at single quotes, which
are used for strong quoting. Let’s print out something
in single quotes using ‘echo’. Inside the single quotes,
I’ll put another ‘echo’. I’ll put something that
might look like a variable– some special shell characters. And when I hit Enter
on this, you’ll see it just echoes back
character for character. No attempts to run commands,
interpret variables, or do anything with
special characters. So that’s what
strong quoting does. Absolutely no
interpretation at all. The only problem occurs when
I want to actually print out a single quote. Usually, I use backslash
to escape the single quote, but special characters
are not interpreted within single quotes. I must end the single
quoted string– so if we’re going to print
out ‘the cat’s pyjamas’. So I want ‘the cat’– and now I want a single quote– an apostrophe– so I must end the
single-quoted string, print out an escaped
single quote, start another
single-quoted string. And I’ve got ‘s’ and ‘pyjamas’. ‘The cat’s pyjamas’. Then we have weak quoting– or double quotes, which
will interpret variables– and also back quotes. So say I’ve put my
name in a variable. We’ve got– my name is Helena. Cool. And I want to use
this to print out a little message to myself– ‘echo “Hello $NAME”‘ And we’ve got ‘Hello helena’. So within the double
quotes, the variable gets interpreted
and printed out. Now I could have done that exact
same thing without the quotes at all and got the same
result. So if I do ‘Hello $NAME’ now, same thing– ‘Hello helena’. The difference is,
in the first case, this whole thing is
treated as one argument to ‘echo’– one long
string with spaces in it. But in the second case, we’re
giving ‘echo’ two arguments– this word and then a
variable to print out. We can see this more clearly
if I put a bunch of spaces in between. If I do it with no quotation
marks, the spaces aren’t kept. But if I do it inside
double quotes like this, the spaces are kept. Since double quotes limit
what get interpreted, they can also be used to
print out some special shell characters. Say I want to print out a
friendly smiley face along with my little message. So I’ll do ‘Hello $NAME’
and a smiley face. Cool. Works perfectly. However, if I didn’t
use the double quotes, it would come up with an error. Let’s quickly show
you the error. Oh, no. Syntax error near
unexpected token. And this is because
the closing bracket is a special character in shell. We often use double
quotes around variables if we suspect those
variables may contain spaces that we want to preserve,
or special characters that might get interpreted. Back quotes are quite
different from the other two types of quoting. They’re used for
command substitution. Anything with back
quotes will be run as a command and
the output captured. I have here, the full text
of ‘The Hobbit’ in a file. And we can use word count,
‘wc -w’ and give it ‘The Hobbit’, and it will tell me how many
words there are in ‘The Hobbit’. Say I want to store this
output in a variable. I surround the command
I’m interested in running with back quotes. And then– well, we’ll
call it ‘$num_words’. I can set a variable equal to
the output of that command. Beautiful. I can then print out my
variable in a sentence using double quotes like before. So we’ll say ‘The Hobbit
has $num_words words’. Ah, ‘$num_word’
rather than ‘words’. There we go. ‘The Hobbit’ has around
97,000 words in it. Or since, remember that double
quotes will also interpret back quotes inside them, I can
skip the variable altogether and just use back quotes
to call that command inside the double quotes. So we have– what was it? ‘wc -w’,
and give that ‘the_hobbit.txt’. Exact same thing happens. As an example of
where I might not use double quotes
around variables, or where I might
want to, let’s say that I’m going to
use ‘egrep’ to find all occurrences of a
certain phrase in both ‘The Hobbit’, which I have here,
and ‘Pride and Prejudice’. The arguments I give
to ‘egrep’ are the phrase that I want to look
for– in my case, it’s going to be looking for
places when people are called “my good sir”. And the files that
I want to look in. So we’ve got ‘Pride and
Prejudice’ and ‘The Hobbit’. There’s also a video
on using ‘egrep’, if you want to check that out. But what if, instead of
giving it an actual phrase, I want to give it a variable? Say, search for this
variable, which holds a phrase, in these few files. In that case, let’s
make a variable. So we’ll call the
variable ‘search’. And it’s going to be
‘my good sir’ still. And now when I call ‘egrep’,
instead of giving it the phrase “my good sir,” I’m
going to give it the variable, but I must surround the
variable in double quotes. So there we go. Exactly the same thing happened. We got one instance of “my good
sir” in ‘Pride and Prejudice’ and one instance in ‘The Hobbit’. However, let’s
have a look at what happens if I don’t put
double quotes around there. And this is going to
get a little bit messy. Oh, no. So what we can see here, it
is matching instances of just “my”. So because that variable
had spaces in it, when I gave it to egrep without
double quotes around it, it took those spaces to
mean “my” as one argument, then “good” as one argument, and
then “sir” as another argument, and ‘egrep’ doesn’t work this way. I have to give it one argument,
one big string for it to search on. Say if I also wanted
to use a variable to ask it to search– or a
variable that holds which files I wanted to search through. So we’ll make that
variable– it’ll be books– and we can actually use our
back quotes here– books. It’s going to be all of the
files here that start with anything, ending with ‘.txt’. Cool. In this case, I don’t want
to surround that variable with double quotes. Let’s put it in here
and I can show you. So ‘$search’ surrounded by
double quotes, because it’s going to have spaces in it, ‘$books’, not surrounded
by double quotes. And we’ll see that works. Again, in this case, I do want
it to interpret my variable as several different arguments. My first argument
is the first book– second argument is
the second book. If I put double quotes
around that ‘$books’ variable, it will complain that
this file does not exist. That is, ‘pride_and_prejudice.txt,
space, the_hobbit.txt’– that doesn’t exist. So those are cases
where I may or may not want to surround variables
by double quotes. So, hopefully, now you have
a bit more of an insight about quoting in shell. [MUSIC PLAYING]

3 thoughts on “COMP2041/COMP9041 – Quoting in Shell Scripts

Leave a Reply

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