BASH Shell Environment (Demo)

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
>> Hey, Cybrarians, and welcome back to
00:00
the Linux+ course here at Cybrary.
00:00
I'm your instructor Rob Gills.
00:00
In today's lesson, we're going to be
00:00
talking about the BASH shell environment.
00:00
Upon completion of this lesson,
00:00
you're going to be able to understand
00:00
the BASH shell variable types as well as
00:00
determine how to view environment variables and
00:00
where to save variables that we want to be persistent.
00:00
Finally, we're going to see how we can configure
00:00
the path to ensure script locations are found.
00:00
Environment variables store information for the shell,
00:00
and they store things like system name or host name,
00:00
maybe the name of the user logged in
00:00
or their default home directory,
00:00
and also the path to find executable programs.
00:00
If that sounds familiar, it should.
00:00
We've talked about that as the path,
00:00
and we'll talk about that again later in this lesson.
00:00
Now in BASH, there are two
00:00
>> different kinds of variables.
00:00
>> The first one is the local variable,
00:00
and these are only available in
00:00
the open show that we're in or in the script.
00:00
For example, on the command line we can set
00:00
variable assignment by just saying a equals something.
00:00
Then we can see the contents of that a variable that
00:00
we've assigned the value of something by doing an echo.
00:00
We do echo dollar sign a,
00:00
and we can see that it just displays
00:00
>> the word something.
00:00
>> However, if you open
00:00
another command line or
00:00
BASH shell, that variable is gone,
00:00
you're not gonna be able to do echo dollar a,
00:00
it won't return anything, it just be blank.
00:00
These are known as local variables.
00:00
But these also referred to those used
00:00
>> or set in a script.
00:00
>> Those are only available during the script execution.
00:00
When you run a script that variables are used
00:00
for that during that script execution,
00:00
when you close it, you can't
00:00
refer to those variables anymore.
00:00
To display local variables,
00:00
we can use the set command.
00:00
Now, global variables by comparison are
00:00
available to all shells and
00:00
get inherited from the shell environment.
00:00
These are also known as environmental variables.
00:00
Now to make variables persistent,
00:00
it should be added to the BASH configuration file.
00:00
It can be added to the user Bash configuration file,
00:00
or if a variable should be available for all users,
00:00
that can be set in as a profile.
00:00
For example, our goofy a or something,
00:00
we could add that to our
00:00
user home directory, tilde/.bashrc.
00:00
We can set export
00:00
a equals something inside of that file,
00:00
and then we can use it anywhere as our user.
00:00
But if we wanted a variable like
00:00
a legit variable to be used for all users,
00:00
we would set that in etc profile.
00:00
Then to display these persistent
00:00
>> environmental variables,
00:00
>> we can use the commands env or printenv.
00:00
A global variable that gets set
00:00
for all users is the PATH.
00:00
We've talked about this before.
00:00
The PATH variable contains a list of
00:00
directories that are used
00:00
to find the location of a command.
00:00
If Bash doesn't know something,
00:00
if it doesn't know that command,
00:00
it's going to look in your PATH statement
00:00
to see if it can find it somewhere else.
00:00
PATH is also really helpful
00:00
for us and for our purposes in this lesson,
00:00
when you want to refer to an executable without having
00:00
to use the absolute path to that executable.
00:00
For example, the absolute path to
00:00
the my script file that lives
00:00
my user's home directory is home/rob/myscript.sh.
00:00
If I were to add home rob to the path,
00:00
then I only have to use myscript.sh
00:00
>> from anywhere because
00:00
>> Bash will know to look in
00:00
my PATH statement to find the location of that script.
00:00
Let's see these variable types and configure
00:00
the PATH with some demo time.
00:00
[NOISE] Here we are
00:00
in our demo environment and today we're
00:00
going to be playing around in Ubuntu.
00:00
We're going to go ahead and open
00:00
the terminal window and make it a little bit bigger.
00:00
The first thing we'll do is set a variable.
00:00
For example, we could do a equals new variable.
00:00
[NOISE] What I'm doing here is I'm assigning a string,
00:00
which is anything inside the quotation marks,
00:00
to a variable which we're going to call a.
00:00
Now to see that, I can do an echo and I can
00:00
do an echo with dollar sign a.
00:00
What that's going to do is to expand
00:00
the variable and display the contents,
00:00
which is new variable.
00:00
It's just a string that I have assigned to it.
00:00
We can also see our variable.
00:00
Remember, when we're using
00:00
these local types of variables,
00:00
we could do that with set.
00:00
I'm going to do a set and grep
00:00
for any variable that starts with a.
00:00
We can see that a equals new variables, so it's there.
00:00
But however, what happens if we open
00:00
another window or another tab
00:00
and try and display the variable?
00:00
What happens then? Well,
00:00
now we can do echo dollar sign a and we get nothing.
00:00
Because this is an example of a local variable,
00:00
is only good for the shell that we're in,
00:00
or it only happens to exist when we're
00:00
>> running a script.
00:00
>> If you want to make this variable persistent,
00:00
and let's go ahead and close this tab
00:00
and go back to original one,
00:00
what we could do is we could add
00:00
this a equals new variable
00:00
into our user bashrc file.
00:00
We can have that be persistent between reboots.
00:00
We can also add this to etc profile
00:00
for legitimate [LAUGHTER] variable.
00:00
But for the purposes of this, we're just doing a demo.
00:00
What we could do is we can go into vim tilde/.bashrc,
00:00
which is my user bashrc.
00:00
There we go. Then down at the bottom,
00:00
what I can do here is I can add that same syntax.
00:00
I can copy an a equals new variable,
00:00
but you'd need to use a keyword
00:00
first, the export keyword.
00:00
Then I can go ahead and hit
00:00
Escape colon WUQ to exit out of vim.
00:00
Now if I open a new terminal session or a new tab,
00:00
I can do echo dollar a,
00:00
and it's available everywhere.
00:00
It's available to my user from
00:00
my bashrc file everywhere it
00:00
knows what that variable is
00:00
because it's been stored persistently.
00:00
Now that is persistent,
00:00
we can see persistent variables by using
00:00
the env command or we can do printenv.
00:00
What we're going to do here, because this
00:00
displays quite a bit of information,
00:00
is we'll do the same trick we did before.
00:00
We'll do prinenv and then grep a.
00:00
Now we can see a equals new variable because
00:00
this is stored in a global variable setting,
00:00
it gets stored in my bashrc file.
00:00
Next, let's take a look at the path.
00:00
Let's go back to my user home directories,
00:00
so just cd tilde.
00:00
This is going to be my user home directory.
00:00
I can verify that with present working directory.
00:00
Inside of this location,
00:00
we can find a file called testscript.sh.
00:00
This is the test script that I
00:00
wrote in this directory and I
00:00
can source it or run it by running testscript.sh,
00:00
so./testscript.sh, and we can see that it runs.
00:00
It just displays a message,
00:00
this is a test script to the console.
00:00
Now, if we wanted to though,
00:00
we can't run it from another place.
00:00
I can't go to home and just say, hey,
00:00
run testscript.sh,
00:00
because Bash doesn't know what that command is.
00:00
It doesn't know where to find that command.
00:00
But what I could do is I can add home/rob
00:00
to the PATH statement and it will
00:00
know to run that from anywhere.
00:00
What I can do is I can go into my profile,
00:00
my user profile, so I can go into dot profile.
00:00
What I can do in here is at the bottom,
00:00
I can set up export statement.
00:00
What I'm going to do is I'm going to export the PATH,
00:00
and I'm going to say dollar sign PATH,
00:00
and I'm going to append to it home/rob.
00:00
Then what I'll do is I will save and quit out of here.
00:00
Now unfortunately, what we have to do is
00:00
we have to log out and log back in.
00:00
I'm going to close all the windows
00:00
and I'm going to hit "Logout".
00:00
What we'll do here is we'll wait a couple of
00:00
minutes and I'll get logged back in.
00:00
Not even a couple of minutes,
00:00
hopefully a couple of seconds here.
00:00
Once I get logged back in,
00:00
what we'll do is we'll display the PATH statements.
00:00
Let me open the terminal again here.
00:00
Right now I'm in present working directory
00:00
home/rob desktop.
00:00
But if I do an echo on dollar PATH,
00:00
because remember this is a variable,
00:00
I can just do echo dollar
00:00
to get the contents of the variable,
00:00
we see here that home/rob
00:00
has been added to the end of the path.
00:00
Remember, before I had to do
00:00
home/rob/testscript.sh where I had to
00:00
be in the home directory to run it.
00:00
What happens if I just run testscript.sh
00:00
from right here? Well, it runs fine.
00:00
I could run this anywhere if you go to temp.
00:00
I could run test script. It runs fine from
00:00
>> there as well.
00:00
>> Once it's in your path,
00:00
Bash knows where to find it
00:00
and you can run it from anywhere.
00:00
With that, in this lesson,
00:00
we covered the two different BASH variable types,
00:00
which are local and global.
00:00
We talked about how to view variables
00:00
and where to save variables for persistence.
00:00
Then finally, we saw during the demo
00:00
>> how we can configure
00:00
>> the PATH variable to ensure script locations are found.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next