This course will be retired on March 4, 2019. We recommend "Introduction to the Terminal" for up-to-date content.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Environment variables store configuration information on our computers. Here, we learn how to read and write values to the environment.
Commands
-
VARIABLE=value
- set a local environment variable -
export VARIABLE=value
- set an environment variable that will be visible to child processes -
env
- view environment variables -
bash
- start a new session within your current session -
echo
- display the arguments sent to echo
-
0:00
? music playing? [Deep Dive] [Console]
-
0:06
We've seen that our programs and commands can take arguments
-
0:09
and options as the input that affects the behavior of the program.
-
0:12
But there is another thing that a program can use to change
-
0:15
the options around--
-
0:17
the environment.
-
0:18
Specifically, environment variables.
-
0:21
Environment variables are just like variables in any
-
0:23
programming language in that they store
-
0:26
a value associated with a name.
-
0:28
When it comes to the console,
-
0:29
our environment variables will be written in all upper case
-
0:33
and the values they hold will be strings.
-
0:35
We can create our own environment variables,
-
0:38
but there are many that the system uses and expects to be defined.
-
0:41
Some of these are home, path, and PS1.
-
0:45
Home is the path to your home directory,
-
0:48
and the path variable is a list of locations where programs are stored.
-
0:53
The PS1 variable defines the format of your command line prompt.
-
0:56
Changing any of these variables will change how the system behaves.
-
1:00
Let's take a look at how to work with environment variables.
-
1:03
Our console session has many environment variables already set.
-
1:09
If we want to explore what those variables are,
-
1:12
we can use the command env for environment.
-
1:15
Now by running this,
-
1:17
it will print out all of the environment variables.
-
1:20
You'll notice that each line takes the format of a name, equals, and then some value.
-
1:25
The names are the names of our environment variables,
-
1:29
and you'll notice that they're all capitalized.
-
1:32
This is a convention for environment variables to be in all caps.
-
1:36
On the right hand side of the equal sign is the value that
-
1:39
that variable currently holds.
-
1:42
Now many of these are set by different configurations files on our system,
-
1:46
but if we take a look at a few of them they might seem familiar.
-
1:49
For instance, you'll notice our home variable,
-
1:52
which currently has the value of /home/treehouse.
-
1:56
Another interesting one is our PS1.
-
1:59
This defines what our prompt is.
-
2:01
Right now it holds the value of /u /w and the dollar sign.
-
2:07
These backslash characters are special sequences that allow us to
-
2:12
insert dynamic information into our prompt,
-
2:15
and it's used by bash to dynamically create this prompt.
-
2:18
So our prompt isn't defined to literally say treehouse or literally have the tilde.
-
2:24
Instead when the prompt is actually printed,
-
2:27
this /u is replaced with our user name
-
2:30
and the /w is replaced with our current working directory.
-
2:34
This is what allows our prompt to change as we move around.
-
2:38
You'll notice our prompt just changed.
-
2:43
Now many of these we don't have to worry about.
-
2:45
For instance, this long one here is set by
-
2:48
some configuration file and it actually
-
2:50
defines the colors that are used when we
-
2:52
use the ls command.
-
2:53
If we were to change this particular string,
-
2:56
we would get different colors based on the different file types.
-
2:59
Some of this is set by the environment that we are logging in with.
-
3:02
But this nth command isn't really what we use the environment variables for.
-
3:07
This is just a useful command for seeing what our current environment is.
-
3:11
Let me clear out the screen.
-
3:13
We can actually retrieve the values stored in our environment variables
-
3:18
from our command line.
-
3:20
The way we do this is with the dollar sign
-
3:23
and the name of the environment variable.
-
3:25
When we use such a thing,
-
3:27
it will be replaced as though the value
-
3:29
was placed into the command, not the variable itself.
-
3:32
I'm going to use a command called echo.
-
3:34
Let me show you how it works.
-
3:36
It literally just echoes back the arguments that we give it.
-
3:40
So for instance if I said echo hello,
-
3:42
the command will print out hello.
-
3:46
But if we use an environment variable,
-
3:48
echo $HOME,
-
3:51
the command line will expand $HOME into the actual value
-
3:54
before evaluating and passing to to echo,
-
3:59
which means echo won't receive the characters $HOME,
-
4:03
instead it will receive the actual value.
-
4:06
So if we do echo $HOME,
-
4:08
it's going to print out what it got,
-
4:11
which is /home /treehouse,
-
4:13
which means any time we use an environment variable like this
-
4:16
it allows us to use the value of that variable.
-
4:19
So if we were to do cd $HOME,
-
4:23
what would happen?
-
4:24
Well this is equivalent to calling cd /home /treehouse.
-
4:29
So it would take us home.
-
4:31
Let's clear this out again.
-
4:33
We remember there is another variable called PS1.
-
4:37
If we want to see the current value,
-
4:39
we can use the echo command again.
-
4:41
PS1.
-
4:42
We can see the value printed out.
-
4:45
Now this variable is set in the configuration file
-
4:48
that's run whenever we start a new session.
-
4:51
But we can actually change PS1.
-
4:53
The way we do that is by typing the name of the variable
-
4:57
PS1.
-
4:58
This is without the dollar sign.
-
5:01
When we're setting it, we do not use the dollar sign.
-
5:03
However, when we are expanding it, we do use the dollar sign.
-
5:07
So the dollar sign is not part of the variable name itself.
-
5:09
Its actually what specifies that we want to expand it out.
-
5:13
So when we're setting it, we give it the name
-
5:16
without any spaces.
-
5:17
We put an equal sign,
-
5:18
and now we can give it a value.
-
5:20
Now we can use quotes around it,
-
5:22
which will make it slightly easier for us to use.
-
5:24
Quotes on the command line allow us to use some characters
-
5:27
that would normally be expanded differently,
-
5:29
but allow us to use multiple words and some other special characters.
-
5:34
So if I were to simply put /w
-
5:38
and maybe we didn't want a dollar sign--
-
5:42
we wanted a greater than sign--
-
5:43
we could do this.
-
5:45
Now as soon as I change this,
-
5:47
you'll notice our prompt change.
-
5:48
That's because the prompt is always using the value of PS1
-
5:52
to determine how the prompt will be set.
-
5:55
We could change it to whatever we want.
-
6:03
Now our prompt is changed.
-
6:04
Now if we wanted to see if this was really set,
-
6:07
we can echo PS1,
-
6:10
which would of course give us the prompt.
-
6:13
This prints out the value of PS1.
-
6:16
Then this is our actual prompt
-
6:18
Because they're the same thing,
-
6:19
it appears that it printed out twice.
-
6:21
But this one is the output of the echo command,
-
6:24
and this one is our actual prompt.
-
6:26
If we were to do env and find PS1,
-
6:30
we can see that the value has been updated.
-
6:34
Let me go ahead and clear the screen.
-
6:36
Now here's something interesting.
-
6:38
We talked about how bash is the program we're interacting with.
-
6:41
It's the thing that is actually interpreting the prompt
-
6:43
and printing it out and handling our keystrokes
-
6:46
and things like our tab completion.
-
6:48
Bash is actually a program that's started for you
-
6:51
when you connect to the machine.
-
6:53
You don't start it yourself.
-
6:54
However, you can start a new session.
-
6:57
If I type in bash, it looks like it changed our prompt back.
-
7:01
But what's actually happened is we've started a new instance of bash.
-
7:05
Our old one is still running with our old prompt.
-
7:08
However, inside of it we're now running a new version, and
-
7:11
because our prompt is set in a script that is run as soon as
-
7:15
we start up our bash script,
-
7:16
our changes to PS1 have been erased,
-
7:19
at least in our session.
-
7:21
If we take a look at the environment,
-
7:23
we can see PS1 has been reverted.
-
7:26
Now I'm in a bash right now,
-
7:29
and if I were to exit it by using the exit command,
-
7:33
this is going to stop the instance of bash that I started a moment ago
-
7:37
and take up back to the original one we connected with,
-
7:40
which is still running.
-
7:41
You'll notice our prompt changed.
-
7:44
Now exit will take you out of your prompt.
-
7:46
Now we're kind of at the top level now.
-
7:48
We're at the bash that was started for us when we connected to our machine.
-
7:52
If we were to type it again,
-
7:53
it's actually going to stop that,
-
7:55
which will actually disconnect us.
-
7:57
You'll notice our tab disappeared because our connection is gone.
-
8:00
If that ever happens,
-
8:01
just go ahead and click plus there to create a new tab
-
8:03
and give it a moment to connect.
-
8:05
Now we've created a whole new session,
-
8:09
and of course our PS1 has been updated.
-
8:12
We can set any variables that we want.
-
8:14
For instance, if I wanted to create an environment variable
-
8:17
called message,
-
8:18
all I have to do is set message equals hello world, and
-
8:23
I can use this however I want.
-
8:25
I could do echo $MESSAGE,
-
8:29
and our message would be printed out.
-
8:31
Now here's something--
-
8:33
what if I start up a new bash session?
-
8:36
In this one, I've set message to hello world.
-
8:38
If I open up a new bash,
-
8:41
it appears nothing has changed,
-
8:43
but now we're in a new copy of bash.
-
8:45
If I were to echo $MESSAGE,
-
8:50
we get nothing.
-
8:52
Now this is interesting.
-
8:53
We created an environment variable.
-
8:55
However, when we started a new process--
-
8:58
a new bash--
-
8:58
it went away.
-
9:00
This is not because it was overwritten in some file like our PS1 is.
-
9:04
It's because our message variable by default will stay in its own session
-
9:09
or its own process.
-
9:11
Now the bash that I created on this line is
-
9:13
a new process that is a child of our original one.
-
9:17
However, the message was not passed down to it in the environment.
-
9:21
We can, however, do that.
-
9:23
So I'm going to go ahead and exit the new bash that I created
-
9:27
just a moment ago.
-
9:28
Now we're back at our original bash session.
-
9:32
So if I were to do echo message,
-
9:34
we'll see it's still there.
-
9:36
It's actually a very important feature to allow our variables to be
-
9:41
passed down to processes that are created by our current session
-
9:45
or our current process.
-
9:47
The mechanism that we do this with is called exporting.
-
9:52
Right now, message is a local environment variable,
-
9:55
which means it's only visible to us.
-
9:57
If we run another program,
-
9:58
programs have access to our environment.
-
10:02
But it only has access to the environment variables that are exported
-
10:06
from the session that created it.
-
10:08
Since message isn't exported,
-
10:10
it's not going to show up as we saw when we created our bash.
-
10:13
So if we wanted to make message export
-
10:16
to any processes that we run
-
10:18
we can do export MESSAGE equals "Hi world."
-
10:27
Now we can still access it the same way--
-
10:29
echo MESSAGE,
-
10:32
and we see "Hi world."
-
10:34
However, if I start a new bash process from this bash process,
-
10:39
where before we lost our message.
-
10:42
If we take a look at echo MESSAGE,
-
10:47
we'll see that the environment variable
-
10:50
has been transferred down to us.
-
10:51
It's been communicated down to the new bash that we created.
-
10:55
So environment variables or custom environment variables
-
10:59
that aren't set by the system
-
11:00
are particularly useful for controlling programs.
-
11:03
Programs we write and other people write
-
11:06
have access to the environment variables of its own process,
-
11:10
which means any environment variable exported will be available to that program.
-
11:15
This provides us a mechanism for communicating
-
11:18
and configuring our programs.
-
11:21
For example, the LS command
-
11:23
uses the environment variable LS colors
-
11:26
to determine how things are colored.
-
11:29
You'll notice that our current configuration
-
11:31
has directories colored blue.
-
11:33
But different file extensions will have different colors.
-
11:36
This is what we see here.
-
11:38
The colors that we see here aren't hard-coded into LS.
-
11:42
Instead, it's configured by our environment.
-
11:45
These variables are often used in all sorts of programs that we write.
-
11:48
For instance, to configure port numbers or environments
-
11:52
or even credentials to data bases--
-
11:54
you can store in the environment,
-
11:56
which allows you to store information
-
11:58
not in a configuration file
-
11:59
but in your computer's environment itself.
-
12:02
One last environment variable that is very important for you to understand
-
12:06
because it's the one you'll probably be modifying the most
-
12:09
is called the path.
-
12:11
Let me clear the screen.
-
12:13
If I echo an environment variable called path,
-
12:18
we're going to see this string.
-
12:20
This is a list of directories separated by colons.
-
12:25
What this represents is the list of directories to search for
-
12:29
when we run an executable.
-
12:31
For instance, when we run echo,
-
12:34
this is a command.
-
12:35
It's a program that actually exists as a file on our computer.
-
12:39
We can find out by typing which echo
-
12:42
exactly where it exists.
-
12:45
It exists as a file in /bin/echo.
-
12:49
Now we can run that by typing /bin/echo
-
12:55
and give it some information.
-
12:57
This is a way we would run our programs with absolute paths.
-
13:01
However, nobody wants to type the full path
-
13:04
to every command that they want to run.
-
13:06
Instead, we simply want to type the name of the command.
-
13:09
Well the way we do that is by using the path.
-
13:12
If we type a command
-
13:13
and it's not the full path to a command,
-
13:15
our computer will search in order of all of these directories,
-
13:19
looking for the echo executable.
-
13:22
It will first look in /user /local /sbin.
-
13:25
If there is no echo there,
-
13:28
it would go to this directory
-
13:29
looking for echo until it finally finds it.
-
13:31
As we see, bin is actually in /bin,
-
13:34
which is in our path.
-
13:36
Now the reason we use this is because sometimes we install executables
-
13:41
that are not in any of these directories.
-
13:43
However, we want them to be available to us.
-
13:46
We went them to be in our path.
-
13:48
So we can update this fairly easily.
-
13:51
From left to right is the search order.
-
13:53
So if we wanted to set a high priority.
-
13:56
For instance, we created a new folder
-
13:58
called bin in our home directory.
-
14:00
If we wanted to put files in here,
-
14:04
which will be programs that we could run without giving the full path,
-
14:07
we would have to add this to our path.
-
14:09
So we would do export PATH equals,
-
14:13
and we want to modify it.
-
14:14
So one way we can do that is by giving our path--
-
14:18
we'll say /home/treehouse/bin.
-
14:23
This is the directory we want at the front of our list.
-
14:26
Each entry is separated by a colon.
-
14:29
Then we can tack on the rest of our list,
-
14:32
which is still in dollar path.
-
14:35
So what we are doing is we're saying
-
14:36
we're going to give path a new value.
-
14:38
It's going to be this path--a colon.
-
14:40
Then we are going to just put in the rest of the path.
-
14:44
So we don't have to type in the old one to modify it.
-
14:47
So if we run that and we check our path
-
14:50
by typing echo path,
-
14:52
we'll see our path has been updated now
-
14:55
If we were to put executables in this bin directory, it would be accessible
-
14:59
just like any other executable.
-
15:02
You'll notice if we type in bash and start a new process,
-
15:05
or we were to log in again,
-
15:07
and take a look at our path,
-
15:11
you'll notice it's been transferred down.
-
15:13
However, this isn't how we want to do this
-
15:16
because usually we have a start-up script
-
15:18
that will set the path itself.
-
15:20
Instead, what we want to do is place it in a file that will run.
-
15:24
If we were to close this session and start a new one
-
15:28
and we were to check out our path.
-
15:32
Now we wouldn't want to normally just export the path from our command line
-
15:35
because if the computer restarted
-
15:37
we would lose all of that--
-
15:38
it's not stored anywhere.
-
15:39
Instead, there is a file that exists
-
15:41
that will run every time we start up bash.
-
15:44
If we do ls - la,
-
15:46
we'll notice that there is a file called bashrc.
-
15:49
Now depending on your system,
-
15:51
this might be a different file
-
15:52
and you'll have to look up specifically which one you need to edit.
-
15:55
On some systems it's .bash profile.
-
15:58
If you're using a shell that is not bash,
-
16:00
it's going to be a completely different file.
-
16:02
But we can actually edit it.
-
16:04
So if I type in nano .bashrc,
-
16:08
this is a file that runs every time we start up.
-
16:12
Now most of this we don't have to worry about.
-
16:15
If I scroll down to the very bottom,
-
16:18
you'll notice our PS1 is exported.
-
16:21
Generally what you will do whenever you want to add something to your path
-
16:25
is type export PATH equals /home/treehouse/bin and path like that.
-
16:36
Usually you're doing this because a program you're installing
-
16:39
has asked you to update the path,
-
16:41
so you will update the path as needed.
-
16:43
But when you save this out,
-
16:45
the next time you log in that will run
-
16:48
and it will update your path for you.
You need to sign up for Treehouse in order to download course files.
Sign up