It used to be that to use Linux you needed to immediately familiarize yourself with the command line. Desktop environments like GNOME and KDE have made using Linux much easier for beginners. You could use Linux for a long time without knowing a single terminal command.
Eventually, though, you’ll want to do something that will require use of the command line, and once you dive in here, you’ll eventually need to know about environment variables. This might seem like an arcane term, but environment variables are easy to understand.
What Are Environment Variables?
Environment variables let you set options across various programs without having to edit a given app’s configuration file. These are system-wide, so they don’t only take effect in programs but in shells and even child processes as well.
These are used to help various commands know where your home directory is, what your username is, and the shell you’re currently using. You can also set various options like terminal colors using environment variables.
Listing Environment Variables
In practice, you’ll probably spend much more time setting various environment variables than listing them. That said, there are times where you’re troubleshooting a problem when it will be very helpful to know how certain variables are set or whether they’re set at all.
This comes in two parts. You can list all current environment variables or choose to look at just a single variable’s value. To list all of the currently set environment variables, run the
printenv command with no arguments:
This will give you a high-level overview of the various environment variables. You can use this to see what has been set, or you can use it with another tool like grep to search for certain strings of text.
If you just want to see what the value of a certain variable is, run
printenv with the variable name of your choice as the argument:
If instead you want to check a few different variables, pass them all as arguments like the following example:
printenv HOME PWD
To pass the value of an environment variable, reference it with the
$ character like the following example:
Setting Environment Variables
Setting an environment variable is also quite easy. Use the name without the
$ operator and assign using the
= operator. For example, to set “EXAMPLE_VAR” to “hello,” you would run the following:
Now you can access this variable as above by using the
This will only set the variable for your current session. Once you log out or restart the computer, this variable will disappear. This is good for testing or if you only need to temporarily set a certain value.
To ensure these environment variables persist, you need to place them in the appropriate configuration file. You can use the “/etc/environment” file for system-wide variables, while “/etc/profile” sets shell variables.
For your own personal use, you can set variables in “~/.bashrc” or a similar file if you use a different shell. To set them, use the same format as above.
With the above command-line tools, you should have the basics for nearly anything you need to do with environment variables. You can start by customizing parts of your shell or other startup options, then expand from there.
Of course, if you’re new to Linux, you need to know about more than just environment variables to make your way around the command line. To help keep everything straight, take a look at our guide to easily memorizing Linux commands.
Our latest tutorials delivered straight to your inbox