[No audio]
Environment variables. What are
the environment variables?
A regular definition or off the book definition of
environment variable is, an environment variable is a
dynamic-named value that can affect the way running
process will behave on a computer.
They are part of the environment
in which a process runs.
So in simple words, environment variables are a set
of defined rules and values to build an environment.
So to make it more simpler I will
give you an example of a house.
We have our house, by the way, it's just a tall
house that's the only house that I could pick out from
one of the search engine that would depict the dining room
and bedroom, anyway, so we have our house, just take an
example of our own house, the house that you live in.
That house has its own environment.
What it means is, it has a bedroom where you sleep,
it has a dining room where you eat, sorry it has
a kitchen where you cook, and the dining room where you
eat, and it has a playroom where you play, and
so many different rooms for other purposes.
Now those are defined a set of rules or someone would
say an unwritten rules. You cannot really sleep in the kitchen,
or you cannot really cook in the bedroom, right?
Because those places are built for that specific
needs, so that's the environment of your house.
Similarly, in Linux, we have environment. Environment meaning
when a user logs in the user is
given an environment by saying, hey,
your home directory is this, your process or your shell where you're going
to run the scripts those paths are these, these are the
coloring you will get for all directory and listing, and
these are a few set of rules that you will
get so they are inherited automatically through the global environment.
Those environments by the user can also be
changed by a specific user as well.
So to view all environment variables in Linux environment
you have to run the command printenv
sorry by the way it's not evn, it's env.
I should fix that and I will definitely will, or
you could just simply type env. So both commands are
the same, we'll give you the same results,
if you wanted to list what is in your environment.
To view one specific environment variable, you could run a
command like echo shell, so $SHELL, if you wanted
to find out what is your shell. Usually the shell
in Linux is given as bash, bin bash, that's a
bash shell, corn shell, korn shell, whichever your shell is.
If you want to find out your
shell, you will specifically do echo $SHELL.
If you want to find out specifically
your path, you'll do echo $PATH.
Anyway, I'll show you in a
few minutes what I'm talking about.
To set the environment variables on the fly for
example you could do export TEST equal one and
then do echo $TEST, then when you type echo
$TEST you will get a result as one.
If you want to set the environment variable permanently then
you will have to modify that .bashrc file, and
there you will have to put in TEST equals single quote, 123, single quote.
Why 123, why not 1? It's just numbers,
I should have picked 1 anywhere to be
consistent, and then you type export TEST.
But if you wanted to set the environment globally and
you want to have it permanently you have to modify
the profile and bashrc that are inside of etc.
Now let me log in to my Linux machine and
I will show you exactly what the shells are.
So I am logged in and I'm logged in as myself,
iafzal, and if I wanted to list the environment
I will type printenv, hit enter, and as you see it's
showing me a lot of stuff, and it's going straight up.
I could do printenv, and pipe it to more
so I could view it one page at a time.
And you will see it will tell you my terminal,
meaning the terminal that will be used to log in,
it would be like an xterm.
My hostname is myfirstlinuxvm.
This is my SESSION ID,
which will automatically be assigned.
This is my SHELL.
Whenever I log in a shell is given to me and
that shell is /bin/bash. The history size, which is whenever
I run certain commands those commands will be kept in history
when you type history command. By the
way, history command is something that I
have covered in the later lectures.
So history command, that is telling you it
has to record up to 1000 line.
So this is the environment.
What is the SSH_TTY terminal that will be given?
What is the USER,
what comes in of course is my user.
What are the different colors?
You see LS_COLORS. When you run ls -l,
you see a different color for directory, different color for
file, those are the colors you could change.
Like every time you put in a gif file, it will
show up in the color code 01;35. What it
refers to, I'm not sure, you can google it,
you could find out. Then going
forward where my MAIL will go.
Whenever someone sends me a mail, where it will
go, it will go to /var/spool/mail/iafzal.
So these are the set rules, set-defined rules.
Where is my PATH?
Every time I run a command, the operating system will
go in and find the location of that command, and
if that location is not listed in one of these
path, it will not find that command.
My own bin directory will be /home/iafzal/bin.
My home directory,
if I run pwd, would be /home/iafzal.
The LANG that is set to UTF-8.
Some HISTCONTROL, my HOME directory, my LOGNAME,
my DATA path, SSH_CONNECTION, and a couple of other
things which I could go over in detail, but I
don't want to take up a lot of your time.
So these are the set variables,
set environment that is given to you.
If you want to check which environment variable, a
specific environment that you wanted to check, you will
do echo and you will do for example, $MAIL.
You always have to start with dollar sign by
the way, because this way it will understand oh,
this is a variable that I'm talking about.
So all I'm trying to find out what is
the path that is defined for my mail.
Right here you see it says MAIL
equals /var/spool/mail/iafzal.
So if my shell is reading the correct
environment variable, I should get this back.
So now if I do echo $MAIL, /var/spool/mail/iafzal. Perfect.
Yes, I am getting that.
Now if I want to do path, I
will do echo $PATH, and it will
list every directory where my path has defined.
Just like that, the path is defined here.
If I wanted to do my home, I will do echo $HOME,
and you will see this is my home directory, right here HOME.
This is my home directory.
Also, sometimes what people do if they are in
one directory, let's say if you are in at etc directory,
you do pwd, you could simply do
cd $HOME, and it will take you home.
That's how your environment is defined as well.
So if you want to set an environment
variable while you're logged into the system. Let's
say you wanted to do TEST equal 1.
So you would type export TEST,
which is variable equal one.
And then when you do echo $TEST, OR
$TEST, you will see the value one.
But of course this is not permanently defined.
If you log out of your system right
now, this environment variable will go away.
So if you want to set this environment
variable permanently in your operating system, then you
would have to actually, vi your bashrc file.
So you are in your home directory,
first make sure, then I will recommend that you copy
your bashrc file, which is .bashrc
and make it bashrc.orig.
So this way if you make a mistake,
you can always revert back to this.
Now you vi your bashrc, and hit enter.
Now come all the way down and define your variable.
So I wanted to define my variable as TEST equal,
now this time I just want to do 123.
When you define a variable inside of the
bashrc, you put a quote, single quote.
Then you hit enter, and then type export TEST.
Go ahead and save your file.
Now if you do test again,
echo $TEST, you'll get one.
Why are you getting one?
Because it is still reading that export TEST equal one
and then it is giving you that same value.
You have to log out and log back in.
So for me I could just go here
and click on this little icon up here
and type in Duplicate Session from my PuTTY.
I will do iafzal, login, hit enter,
and now when I do echo TEST, you will
see it is giving me 123.
Because this session I have a new session,
and when I logged into a new session, every time I
log into a new session, my login will read .bashrc file,
this file, and when you do ls -l, and by the way
should go up, you will see .bashrc is not
showing up, because it is a hidden file.
Anytime you need to view a hidden file, you
have to do ls -la, hit enter.
And now you could go up, and you
will see your .bashrc file right here.
This is the one I modified.
If you have modified, you could modify this file to
set your environment and then make sure you log out
and log back in or start a new session,
if you want to have your
environment variable take effect right away.
And the last one I have right here, I'll
go ahead and close the session behind it, is
if I want to set globally environment permanently.
Meaning if I wanted to set these environment not
just for myself, I wanted to set it for
everyone else who logs into that system,
make sure you know what you're doing first of all.
So I will not recommend you that you
do it right now in your lab.
However, I will explain you the files
that you would need to modify.
Those files are at /etc/profile.
This is the file that you would have to
modify or you have to modify /etc/bashrc.
So you can modify these files and you will put in
exactly the same way you put it in your .bashrc.
But again, as I said, please do not modify
this because this is lab environment, I don't want you
to modify globally, and it will cause problem.
And then you will know the root cannot
log in or yourself cannot log in.
But if you want to do it for your
production environment or your corporate environment where you're working,
I would recommend you to do it in development
first and then you try it in production. All right, good luck.
[No audio]