Linux 101: What are environment variables?

Jack Wallen introduces you to Linux environment variables. What are they and how are they set and unset?

linux penguin

Image: Jack Wallen

How many times have you installed something on Linux, only to find out that you need to set certain environment variables before the installation will work? For me, it happens all the time. 

But what are these environment variables, and how do you set them? Let's dive in and find out. 

SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)

The first thing you should know is what environment variables are. Simply put, environment variables are a set of dynamic named values stored within the system that is used by applications. These variables allow you to customize how specific applications and services behave with the system. Each variable contains a name and an associated value. Usually, the name is in UPPER CASE, and the values are, of course, case-sensitive. 

Say, for instance, you install the Go language on Linux. Because of the way Go is installed, you have to let the system know where the executable binaries are stored. Most often, those files will be found in /usr/local/go/bin. But if you just download and unpack Go to /usr/local/go, your system (and the bash shell) will not know those files are there. To fix that you have to set an environment variable. 

Obviously, you could simply add /usr/local/go/bin to your $PATH, which is the traditional way to solve this problem. But let's say you want to do this temporarily. You could set and then unset the environment variable. First, let's set GO_PATH as /usr/local/go/bin. To do that, we use the export command as in export GO_PATH=/usr/local/go/bin. Issue the command echo $GO_PATH and you'll see /usr/local/go/bin has been successfully set. 

SEE: 5 Linux server distributions you should be using (TechRepublic Premium)

Now, if you want to use the path for Go in a script, you could insert $GO_PATH instead of /usr/local/go/bin. This new variable is available system-wide and is inherited by all spawned child processes and shells. To unset that environment variable, you would issue the command unset GO_PATH

You can also list out all of your currently set environment variables with the command set, which will list out a ton of variables. 

Environment variables are very helpful to use in scripts and are sometimes necessary to be manually set when installing certain software. Start using them now to make Linux and the Linux command line even more efficient.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see