>> [MUSIC] A bash script is a file that contains
one or more commands that is meant to be
executed within your bash shell.
Now you're probably typing in
individual commands right into your bash shell,
which is great for simpler tasks and
when you're getting started, just learning.
But as you get more and more involved with different tasks,
projects, using a script is going to make you more efficient.
Plus, since it's nothing more than
a file, you can share it with peers.
You can share it online and you can leverage it
later on or for other scripts that you'll be making.
Let's now take a look at an example script that I have
here to just better understand the structure of one.
This script that I've got is Hello_world.
It's going to be inside of the what is a bash script directory.
The GitHub link will be in the description.
Let's take this off and cover it line by line.
These first line up here is what we know as a shebang.
How do we know it's a shebang?
It starts with, well,
depending on who you ask,
this is either a pound,
a number sign, hashtag.
I'm going to call it pound. But these two characters
back to back, meaning they're together.
The pound and the exclamation mark means that,
hey, this is a shebang.
Shebang is used to tell the environment that we're in,
which interpreter we're going to be using.
In this case, since the shebang is /bin/bash,
we're telling it, hey, we're going to use bash,
which makes sense because this is a bash shell.
Falling with that we have another line that
starts with a pound character,
but after that we have some text.
In this case it says script that will print out a message.
This is known as a comment.
Comments are used to document your scripts,
provide clarity in context to what you're creating here.
You can place a comment anywhere at the top
or the shebang has to be the first thing first-line,
but you can put it lays come immediately after.
In this case, you can place a comment on the bottom.
The only place you can't place
a comment is in the middle of command.
I can copy this here and put it in the middle here,
because comments make anything that follow them.
Anything that follows this pound
here is not going to be actionable.
It's not going to be interpreted as a command.
If you put it in the middle of the command,
that means your command is not going to perform
the action that it's supposed to do.
Keep that in mind. Following that on line number 5,
we have the echo command.
We know that echo is used to print out some texts.
In this case it is hello, world,
a very common first project when
you're learning new scripting languages
or new programming languages.
Now, this is a very simple bash script,
but it really does contain
the basics of what make up all bash scripts.
We have here, the shebang,
we have comments, and we have commands.
Later on we're going to take a look at
functions, loops, conditional statements,
case statements, and put it all together
to make a much more involved bash script.
But we've got the basics here.
Covering all of that, you may ask,
well, how can I go and execute this?
Now, you might think we'll have to type in the name hello,
world and go ahead and execute that.
But as you can see, we have the command not found.
This is because our script lives in
a directory that is not listed in our path environment.
We've spoken about the path environment before in previous videos.
If I execute echo and print out my path environment variable,
you can see I have a bunch of directories in here,
but the one that I'm currently in,
which is what is a bash script directory is not listed in here.
That means we'll have to use the explicit path since if I
do ls to list the contents of my current directory,
you can see that my script is here.
That means I can use./hello world
to execute this script right, wrong.
Well, this will work once we update the permissions.
Now we're going to use the chmod command
to update the permissions.
But first let's use ls-l to get
the details of the permissions
of this script that it currently has.
Now that first dash here represents a file.
This correct because it's bash script is a file.
Then it looks like we have read permissions for the user
and then everything else is not set.
There's no read, write,
or execute permissions for anyone here. Let's update this.
Let's do chmod 700 is a pretty standard one used for scripts.
Then we provide the name of the file.
Awesome. Now let's do another ls here.
Now we see that the user has read, write,
and execute permissions for the script and everyone else.
The group and the others
don't have any permissions, just fine here.
Now we can go ahead and use that explicit path,
which is again./hello world.
When we run this, we can now see that the message is printed out.
But what if we did want to be able to run hello,
world, hello thread that L here.
Hello_world, well, we would have to
move this or copy the script into,
again, and one of those directories that is in
the path environment variable.
Again, let's print out the path environment
variable and let's see which directories you have available to us.
For user scoped scripts.
This means scripts that you create as
a user and you want to be able to run for yourself.
It's a good idea to place them in
a local bin on your home directory.
In this case it looks like we have
this home/vscode/.local/bin file here.
Now this directory doesn't actually exist.
We have to first create it.
We can do mkdir and we'll do -p.
This will allow us to make any subdirectories that we have to,
and I did know home exists.
I know VSCode exists as well,
but local does not exist.
We're going to type in local.
It's going to create that for us and it's going
to create bin for us as well.
Go ahead and hit "Enter" and then that'll create that.
If I tried to do ls/home/vscode/.local,
we see now it exists there and
the bin directory is in there as well.
I'm going to use copy instead of moving,
copy "Hello_world", which
is the script that we want to move to and look,
I already knows what I want to do.
It's /home, /VSCode/.local/bin.
Again, this is directory that we just created.
It will move that in their fantastic.
Now if I do hello_world, the message now prints out.
Again, this is because the script lives inside of
a directory that is in our path environment variable.
This is just a special variable that holds
all the directories where you can use scripts
and execute them directly.
Awesome, that's it for this video.
Up next we're going to talk about variables. [MUSIC]