Environment Variables
1h 20m

As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, introduce new concepts such as job control, switching users, and installing software. 

Learning Objectives

  • Implement processes and job control, and switch between users
  • Install software using RPM- and Debian-based systems

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • Professionals who want to learn more about Linux to enhance their career prospects


This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.


In this lesson you'll learn what environment variables are, what they do and how to create as well as delete them? An environment variable is a storage location that has a name and a value. They often affect the way programs behave. For example, in earlier lessons you learned how to tell various programs about your preferred editor by setting the editor environment variable.

The easiest way to view all the environment variable names and values is to run the printenv command without any arguments. On your screen you can see an example of this. Each line contains the environment variable name, followed by an equal sign and then the value of that environment variable. For example, the environment variable host name is web01 since that is the host name of the system that I was logged into when I ran this command. This is an example of an informational environmental variable.

Another example on your screen is the path environment variable. This is an example of where the value of the variable affects how a program behaves. In this case the shell uses the path to determine what paths to look for commands to execute and in what order. If you change the path environment variable you change how the shell operates.

If you want to look at the value of one specific environment variable, you can pass its name as an argument to the printenv command. If you were to run printenv home then only the value of the home environment variable will be displayed. Just like most things in Linux, environment variables are case sensitive. If you were to execute the printenv command followed by home in all lowercase letters, nothing will be returned because there is no environment variable named home in lowercase letters.

By the way, environment variables are uppercase by convention. You can create lowercase environment variables but you will rarely see that. Another way to display the contents of an environment variable is to use the echo command. With this method you need to prepend the dollar sign to the environment variable name.

If you wanna look at the contents of the home environment variable, you can run echo, space, dollar sign, H-O-M-E. Again, environment variables are case sensitive, so trying to echo the value of dollar home in all lowercase letters will not return a result.

When you create an environment variable use the syntax of export followed by the environment variable name, followed by an equal sign, and finally, the value you want to assign to the variable in quotations. Do not use spaces around the equal sign. If you do use spaces you'll get an error. Run export, space, editor, equal sign, quotation mark, VI, quotation mark, to create the editor environment variable and assign it the value of VI.

Some commands look at the editor environment variable to know which editor to start. By the way, if the editor environment variable already existed this would override its contents. So this is not only how you can create an environment variable, it's also how you can update an existing one.

Another example is setting the TZ variable. Here, running export, space, TZ, equal sign, quotation mark, US, forward slash, Pacific, quotation mark, sets the time zone to US Pacific Time. The date command for example, will return a different result depending on the value of the TZ variable.

Let's say you no longer need or want an environment variable, to remove it from your environment run the unset command followed by the environment variable name. For example, let's say we want to rely on the default time zone of the system, instead of overriding that with a TZ environment variable. So we remove it by running unset TZ. The act of setting and unsetting environment variables from the command line affects your current running session only. If you want to make your settings persist between logins, you'll want to define your environment variables in your personal initialization files like the dot bash underscore profile file. 

You use the same syntax as you learn. So if you want to explicitly set your time zone, then you can add an export TZ line in your dot bash underscore profile as shown on the screen. If we want to see what environment variables are configured, we can run the printenv command. If there are too many environment variables and they scroll off your screen, you can pipe the output to a pager utility like less.

Now I know what environment variables are configured in my environment. If I wanna look at the value of just one of those variables, I can use printenv or echo. Let's look at the path, so we can run printenv, path, which will display the contents of that environment variable, or we can use the echo command echo, and this time we're going to prepend the environment variable name with the dollar sign. So dollar sign path will return the path.

Let's look at the current time with a date command. Now let's set our time zone by changing the TZ environment variable. Set this to US Pacific Time, and I'll run date again. And then now you can see it's 11:14 instead of 14:14, because in the Pacific Time Zone it's currently 11:14 AM. Now let's try removing the TZ environment variable and see if our time zone is set back to the default.

Now, when we run the date command we're back in the US Eastern Time Zone because that's the default set on this particular server. Let's say that we always want to display the time in the US Pacific Time Zone. To make this setting persist we're going to add it to our bash profile file. So I'm gonna use the VI editor because that's my favorite editor. You can use Nano Emacs or whatever editor you like. And then I'm going to edit this file bash underscore profile and then my home directory. And then I'm just gonna go to the bottom and do an export here, export TZ equals US Pacific. And now I'm going to write and quit, save my changes. I'm gonna log out of the system.

There we go! And now I'm going to connect back in. So now let's see if the time zone variable is set, we can do echo dollar sign TZ, and sure enough it's set to US Pacific. And if I run the date it is in Pacific Time as well. By the way, you usually can tell what environment variables a program uses by looking at its man page and the environment section. Let's do that now for the date command.

So if I run man, and the command which is date, hit enter, and I can look through the man page here. And if I get down to the section here called environment I will see it has one environment variable listed which is capital TZ, which is the environment variable we've been working with. And it says here that it specifies the time zone unless overridden by command line parameters. This is how you can tell what impact an environment variable will have on a given application.

In this lesson you learned that environment variables are name value pairs that can affect the behavior of an application. You also learn how to view your environment with a printenv and echo commands. Additionally, you learned how to create or update environment variables using the export syntax. We talked about removing variables from your environment with the unset command. Next, you learned how to make environment variables and their values persist between sessions. Finally, we talked about how you can find what environment variables a given program uses, by looking at its man page.

About the Author
Jason Cannon
Founder, Linux Training Academy
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.