Jack Wallen introduces you to Linux environment variables. What are they and how are they set and unset?
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.
- Linux 5.14 kernel: New and exciting features coming to the release (TechRepublic)
- Linux 101: Demystifying the Linux directory structure (TechRepublic)
- How to become a developer: A cheat sheet (TechRepublic)
- Kubernetes: A cheat sheet (free PDF) (TechRepublic)
- Microsoft Linux is not what you thought (or hoped) it would be (TechRepublic)
- A guide to The Open Source Index and GitHub projects checklist (TechRepublic Premium)
- Linux, Android, and more open source tech coverage (TechRepublic on Flipboard)