Commenting and 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 plus course here at Cybrary.
00:00
I'm your instructor, Rob Goelz.
00:00
In today's lesson, we're going to be talking about
00:00
commenting and using escape characters
00:00
in our Bash scripts.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to understand
00:00
why it's important to place comments in
00:00
your scripts and we're going to see during our demo
00:00
how to use comments in your scripts.
00:00
Comments are used in Bash scripts for a few reasons.
00:00
We could use them to explain the purpose of the script,
00:00
maybe we want to give some more details
00:00
around who created it,
00:00
who owns the script, and we should also probably put in
00:00
notes about changes that we're making
00:00
to the script over time.
00:00
But why are we talking about this here?
00:00
Well, because the very first line
00:00
after you specify the interpreter should be a comment.
00:00
We can see, for example, here we're
00:00
specifying the shebang,
00:00
>> and then we indicate /bin/bash is
00:00
>> the interpreter we're going to use for the script,
00:00
>> and then the very next line is
00:00
>> a comment on the purpose of the script.
00:00
Now, as you can see above,
00:00
comments begin with the hashtag or pound sign symbol,
00:00
which means that the interpreter is going to ignore
00:00
all text after that symbol and to the end of the line.
00:00
Where should you use a comment exactly?
00:00
Well, besides starting with a comment,
00:00
you really should use comments
00:00
liberally wherever it makes sense.
00:00
A few places that you could use those would be
00:00
anywhere that needs explanation
00:00
or clarification on what the code does,
00:00
>> or to explain your thought process
00:00
>> behind the code that you've written,
00:00
or just to help you remember what the code does.
00:00
I know when I create scripts,
00:00
sometimes I go back at them a couple of weeks or
00:00
maybe a month later and go, well, what does this do?
00:00
Why did I do this? What was the thought process here?
00:00
Using that information,
00:00
using comments is going to help you remember things
00:00
>> and document your code in your Bash scripts,
00:00
>> and you can use a comment on its own line.
00:00
>> We saw that before, you just place
00:00
the hashtag or pound sign
00:00
at the beginning of it and then
00:00
indicates that it's a single line comment.
00:00
You can go ahead and use a comment
00:00
>> at the end of a line.
00:00
>> For example, in this case, we're seeing
00:00
echo "Hello world" and then at
00:00
the end of it we add that hashtag
00:00
and we add the comment information.
00:00
In this case, this is a comment at the end of the line.
00:00
Let's take a look at this with some demo time.
00:00
Here we are back in
00:00
our demo environment and we're going to
00:00
continue on inside of Ubuntu today.
00:00
We're going to go ahead and create
00:00
a new script during this demo,
00:00
but let's do one thing first.
00:00
I'm actually going to navigate into
00:00
the user home bin directory.
00:00
The reason that I'm going to do this is because
00:00
when you echo path on Ubuntu by default,
00:00
you'll see that it adds
00:00
that directory path into
00:00
the path statement automatically.
00:00
If you place anything in
00:00
the bin directory in our user home directory,
00:00
that indicates that we'll be able to run commands from
00:00
anywhere without having to specify the absolute path.
00:00
I'm going to change into that cd /home/rob/bin.
00:00
Now what we'll do is we'll create
00:00
>> a new test script here.
00:00
>> We'll just call this one testscript3
00:00
>> because we've already had,
00:00
>> sorry, 3.SH,
00:00
because we've already had a one and two.
00:00
Now what's the very first thing
00:00
we have to do with any script?
00:00
We have to add in our interpreter,
00:00
shebang and then /bin/bash.
00:00
We're going to be using Bash here,
00:00
and then the very next thing we should
00:00
do is add a comment.
00:00
Generally, the format I like to
00:00
use is I use a pound sign or hashtag,
00:00
whatever you want to call that,
00:00
>> and then I put a space and then I start
00:00
>> writing my comment here,
00:00
>> and so I'll say this script is written by Rob
00:00
>> and its, I'll start a new line,
00:00
>> purpose is to print
00:00
a single message to the screen, and now let's do that.
00:00
Let's give the script something to do.
00:00
What I'll do is I'll say echo and I'll
00:00
say message to the screen.
00:00
Now let's go ahead and add another line,
00:00
>> and we'll just say echo,
00:00
>> this is another line,
00:00
and then to demonstrate, we can also
00:00
use a comment at the end of the line.
00:00
We'll just add a comment right at the end of it here,
00:00
and we'll say this is
00:00
a second line we may use in the future.
00:00
But you know,
00:00
if we're looking at the top of our script here,
00:00
we say that its purpose is just to
00:00
print a single message to the screen.
00:00
We don't actually want to print the second message,
00:00
so you can go back to the beginning of the line.
00:00
I'm just hitting home on my keyboard,
00:00
you can also hit Control A sometimes,
00:00
and I'm going to go ahead and just
00:00
put the comment character,
00:00
that hashtag or pound symbol here at the beginning,
00:00
and now we can see that it's
00:00
basically turned this into a comment.
00:00
It's changed the coloration,
00:00
change it to this light blue color here,
00:00
>> and it's basically canceled out this end character
00:00
>> because what we should remember is
00:00
that the interpreter is going to
00:00
disregard everything after that pound sign
00:00
or hashtag or whatever you want to call it.
00:00
Now we can save and close this,
00:00
we're going to go ahead and hit Escape, hold on WQ
00:00
because we are in Vim and that's how we get out.
00:00
Now we need to do is we need to set up
00:00
>> permissions on this so we can do
00:00
>> chmod, and we'll do u plus x
00:00
>> to make it executable by me,
00:00
>> and we'll say that we want to do that on
00:00
testscript3.SH,
00:00
and now we can run this script,
00:00
>> and we can see that all it prints out
00:00
>> is the message to the screen.
00:00
But if we were to go back into the script again,
00:00
we can see that we have a lot of
00:00
>> other information here,
00:00
>> but it's not going to be printed
00:00
because it's all commented out.
00:00
With that, we've reached the end of this lesson.
00:00
>> In this lesson, we covered why it's important to
00:00
>> place comments in your scripts.
00:00
You could do this to help other people
00:00
understand your code, to clarify,
00:00
or to help you remember what you did and why
00:00
you did it when you go back to the code later,
00:00
and then also we saw how to use
00:00
comments in your scripts during our demo.
00:00
Thanks so much for being here,
00:00
>> and I look forward to seeing you in the next lesson.
Up Next