Escaping Characters

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello Cybrarians, and welcome back to
00:00
the Linux course here in Cybrary.
00:00
I'm your instructor, Rob Goelz.
00:00
In today's lesson, we're going to be
00:00
learning about escaping characters.
00:00
Upon completion of this lesson,
00:00
you're going to be able to explain
00:00
the purpose of escape characters
00:00
as well as understand the rules
00:00
>> for escaping characters.
00:00
>> Then finally, during our demo at the end of the lesson,
00:00
we'll use escape characters in our scripts.
00:00
The last thing to mention before we dig into
00:00
some more advanced Bash scripting
00:00
content is the concept of escaping
00:00
characters because as we now know,
00:00
there are special or reserved characters
00:00
in Bash scripting and these are called metacharacters.
00:00
These are things like the redirection
00:00
operators that we've covered,
00:00
so less than, greater than,
00:00
the ampersand, and the pipe,
00:00
and shell expansion characters that we've covered,
00:00
which are asterisks,
00:00
question mark, all of the braces,
00:00
the dollar sign, and the backtick.
00:00
If we have to use a special character in
00:00
a script and we want to use it as a plain character,
00:00
we have to escape the character special meaning,
00:00
thereby disabling it and not having
00:00
the Bash shell interpret that as a metacharacter,
00:00
instead interpret it as a plain character.
00:00
There's some rules on how to do this,
00:00
on how to escape characters.
00:00
You could use double quotes and
00:00
double quotes they disable or
00:00
escape the special meaning for
00:00
redirection characters such as less than, greater than,
00:00
and pipe, as well as
00:00
all the file globbing characters
00:00
which are the asterisks,
00:00
the question mark, and the
00:00
left and right square brackets.
00:00
However, you can use
00:00
single quotes to disable all metacharacters,
00:00
including redirection and globbing,
00:00
the ones we talked about above,
00:00
as well as command substitution,
00:00
which is the backtick and
00:00
the dollar sign with the parentheses,
00:00
as well as anything with
00:00
variable expansion and substitution.
00:00
Pretty much anything that starts with a dollar sign.
00:00
We can also place a backslash character before
00:00
any single metacharacter to
00:00
escape it or just make it a plain character.
00:00
Now, escape characters are also used when you're doing
00:00
character insertions in echo or awk.
00:00
There are four types of
00:00
escape sequences that are
00:00
commonly used when you're working in echo,
00:00
printf, and awk.
00:00
For example, backslash n inserts
00:00
a new line and returns
00:00
a cursor to the beginning of the line.
00:00
Form feed or backslash f advances to a new page,
00:00
backslash r returns the cursor
00:00
>> to the beginning of the line,
00:00
>> and backslash t inserts a tab.
00:00
Let's take a look at all of this with some demo time.
00:00
Let's play around with escaping
00:00
characters in a new script.
00:00
I've already created a few scripts,
00:00
so we're just going to walk through them here to save
00:00
some time and get through as much of
00:00
this content as we can very quickly.
00:00
What we'll do is we're going to open the first one,
00:00
and this one is just called escCharacters.sh.
00:00
Now what the script does here,
00:00
first of all, obviously at the top, we call bin/bash.
00:00
Hash bin and then bin/bash,
00:00
that's going to be our interpreter.
00:00
Then I have some information on what the script
00:00
does in the three comment lines there.
00:00
Now what we're doing is we're using
00:00
variables to create directories.
00:00
We call four different
00:00
directories that we are going to create,
00:00
and then we pass those to
00:00
the make directory character using
00:00
variable expansion to expand
00:00
the names of each one of
00:00
those directories and create them.
00:00
Then we're going to go down below that.
00:00
We've got three lines that are going to
00:00
create files in those directories,
00:00
it's going to change into those directories,
00:00
and then if that is successful,
00:00
which is what the double ampersand means,
00:00
then we'll create files using touch.
00:00
Then at the end of each one of these lines,
00:00
I have information on what these are
00:00
doing, what they're creating.
00:00
Let's go ahead and escape out of this.
00:00
Then we'll do a chmod u
00:00
plus x on escCharacters.sh.
00:00
Then we can run escCharacters.sh.
00:00
What happens? Well, we have a problem.
00:00
Right off the bar, we find out
00:00
that some of our files can't be written.
00:00
This is because the Bash shell,
00:00
the interpreter, can't work
00:00
around some of the metacharacters
00:00
that are being used in our script.
00:00
Let's actually look at what it did create.
00:00
If we do an ls dash r on home/rob,
00:00
and then the demo there,
00:00
we can see that we only really
00:00
got a handful of things created.
00:00
It didn't create any of the expansion files,
00:00
it created a few of the glob files that it knew how to
00:00
create and it didn't create any
00:00
>> of the redirection files.
00:00
>> Let's go ahead and clean this up a little bit.
00:00
The very first thing we need to do,
00:00
is we need to get rid of all this stuff.
00:00
We'll do r minus rf and I'll just go up one directory
00:00
that's doubled periods and then forward slash,
00:00
and then I'll specify that I want to remove,
00:00
the demo directory in my home
00:00
directory because we're currently
00:00
in home/rob/bin and that was in
00:00
>> home/rob/demo directory.
00:00
>> So that's how you do that, that's a little shortcut.
00:00
Now let's go back into our
00:00
script file and we're going to take a look
00:00
at a second script file that I've created.
00:00
We're going to go ahead and take a look at that,
00:00
that is escCharactersStep2.sh.
00:00
What we've done here is we've actually
00:00
used our escaping rules.
00:00
Because if we're working with glob files,
00:00
we have to escape them by using double quotes.
00:00
You can see that every file that we're creating is
00:00
escaped with a double quote
00:00
before and after the file name.
00:00
So that means that everything
00:00
inside of that double quotes
00:00
will be treated as a plain character
00:00
and not a metacharacter.
00:00
We can do the same thing
00:00
>> for our redirection characters.
00:00
>> We've done that here, we've created File 1,
00:00
File 2 with a greater than sign, and File 1,
00:00
File 2 with a pipe, and those will all be
00:00
created without any issues because we have
00:00
escaped them before we didn't see any.
00:00
Then the same thing with the expansion files, but here,
00:00
remember with expansion files,
00:00
you have to use the single quotes.
00:00
They're one of the ones that require
00:00
you to use a single quote.
00:00
In reality, I could have put all of
00:00
these in single quotes because
00:00
anytime you put something in single quotes
00:00
it escapes all meaning,
00:00
but just to illustrate the differences between those,
00:00
I did these in double quotes,
00:00
and I did the expansion characters in single quotes.
00:00
Let's go ahead and hit "Escape:wq".
00:00
Now we can go ahead and run this.
00:00
We'll run escCharactersStep2.sh.
00:00
Now we can see that completes,
00:00
it runs without any issues.
00:00
We can do the same thing we did before,
00:00
we'll do an ls dash r on home/rob/demo there,
00:00
and now we can see that all of those files got created,
00:00
all the files that we told it to create.
00:00
Great. Now let's move on and work on some other things.
00:00
Part of the best thing to
00:00
do when you're escaping
00:00
characters is use this to print things.
00:00
What we'll do is,
00:00
we'll go ahead and remove our
00:00
demo directory one more time,
00:00
so we'll do r minus rf on the demo directory.
00:00
Now what we're going to do is we're going to go into
00:00
a third step that I've created,
00:00
and this is certainly something you can do too.
00:00
Think about it as you're creating things,
00:00
that you want to do things in steps,
00:00
you want to save things,
00:00
you maybe want to create different versions of
00:00
your file so that you can work backwards.
00:00
Later on down the road, we'll talk
00:00
about version control using Git.
00:00
But for the purposes of this lesson and this module,
00:00
we're just creating things step-by-step as we go along.
00:00
Now what I've done here inside
00:00
of this script is I've done a few things.
00:00
I've gone ahead and created
00:00
a couple of demo directories,
00:00
I've created a couple of different variables up
00:00
here that store all of the different glob characters
00:00
and all the redirection characters.
00:00
Then I'm just doing the same thing we're doing here,
00:00
but now we're going to
00:00
>> try to do is we're going to
00:00
>> try and print a list of files that
00:00
contain an asterisk by
00:00
doing a list on the demo directory,
00:00
a recursive list because we're using this dash r on
00:00
the demo directory and grepping
00:00
for anything that contains an asterisk.
00:00
Then I'm going to go ahead and just add
00:00
this cleanup routine to the end so that
00:00
we don't have to keep doing the r
00:00
minus rf on demo directory,
00:00
it'll take care of it for us
00:00
every time we run the script.
00:00
We're going to go ahead and hit
00:00
"Escape:wq" and get out of this.
00:00
Now we'll try and run escCharacterStep3.sh.
00:00
What happens?
00:00
Well, run, it completed,
00:00
but we don't see any files that have an asterisks.
00:00
Why is that? Well, it's the same thing we just saw.
00:00
We need to use the escaping rules for file globbing.
00:00
We need to put a pair of double quotes around this.
00:00
We could use single or double,
00:00
but since it's file globbing we can use double,
00:00
and then we can hit "Escape:wq".
00:00
If we run this again, now it works.
00:00
We see the two files that
00:00
happen to have an asterisk in them,
00:00
which is file star or file*1.txt and file*2.txt.
00:00
Then we perform the cleanup
00:00
and we're moving stuff in my directory.
00:00
We see the output, but you notice that's scarily.
00:00
We don't see the files on an individual line,
00:00
they're just File 1 and then there's
00:00
another line behind, this is File 2.
00:00
Let's look at how we can
00:00
>> clean that up a little bit more.
00:00
>> We're going to take a look at one more script and
00:00
this one is called escCharactersStep4.sh.
00:00
What we've done in here again is we've
00:00
created these demo directories,
00:00
we also have these rules about the glob character,
00:00
redirection character, and expansion characters,
00:00
these variables that hold all of those characters,
00:00
and you can see each one of these is escape.
00:00
We have the asterisk,
00:00
and then we're escaping the pipe,
00:00
and we're escaping the other things.
00:00
As we go along here, we need
00:00
>> to escape these to indicate
00:00
>> to Bash that we
00:00
actually want to know about the plain characters.
00:00
We're using the pipe operator here,
00:00
so we're escaping the pipe operator
00:00
so that it knows that we actually want to use
00:00
that as an awk instead of
00:00
as being used as a pipe or a metacharacter.
00:00
We use these, we create these rules to look for
00:00
all of these types of characters
00:00
>> that we're looking for.
00:00
>> For example, the star or the asterisks,
00:00
the question mark, and the square brace.
00:00
Then we're looking for anything that contains a greater
00:00
than or a less than symbol, and a pipe.
00:00
The same thing here, we're looking for our braces,
00:00
and we're looking for our parentheses.
00:00
Then we create all the directories like we did before.
00:00
But down here, our printf statements are
00:00
a little bit cleaner because we're using
00:00
this backslash n to indicate that we want to create
00:00
a new line before and after each one of these,
00:00
and then we do our ls minus r on
00:00
the demo directory and we can grep for glob character.
00:00
We're going to basically call this variable up here,
00:00
we're going to expand this to search in
00:00
our demo directory for any file
00:00
that contains a glob character.
00:00
Then the same thing for redirection characters,
00:00
and then the same thing for expansion characters.
00:00
Then when we're done, we'll perform
00:00
clean up and remove the directory.
00:00
>> Let's hit "Escape:wq".
00:00
>> Now we can run this.
00:00
We can run escCharactersStep4.sh.
00:00
We'll see that works as we intended.
00:00
It goes ahead and it goes through and
00:00
finds all of our file with glob characters,
00:00
all of our files with redirection characters.
00:00
It displays any place where
00:00
our other expansion characters are found,
00:00
which we find everywhere.
00:00
Then we also see the expansion files,
00:00
and the glob files,
00:00
and the redirection files.
00:00
Because we're doing an ls minus r
00:00
it looks like this is a little bit goofy,
00:00
but we see all of the information
00:00
>> that we're looking for
00:00
>> and then we clean up and remove the directory.
00:00
With that, we've reached the end of this lesson.
00:00
In this lesson, we covered
00:00
the purpose of escaping characters,
00:00
we talked about the rules for escaping characters,
00:00
and then we use escape characters in
00:00
our Bash scripts at the end of this lesson.
00:00
Thanks so much for being here,
00:00
and I look forward to seeing you in the next lesson.
Up Next