Environment variables are important for managing Linux systems. They allow engineers to customize the system. In this tutorial, we will learn about Linux environment variables, how they are different from shell variables, and how to view, set, and manage them.
What are Environment Variables in Linux?
Linux environment variables are special variables used by a shell and its child processes. They affect various aspects of a Linux system. For example, the SHELL variable defines the default shell for a user. Administrators often use the JAVA_HOME= variable to specify the version of Java for programs on a system.
Here are a few important things to remember about Linux environment variables:
-> Environment variables follow the format <NAME>=<VALUE>.
-> You can assign multiple values to a variable by separating them with colons, like this: <NAME>=<VALUE1>:<VALUE2>:<VALUE3>.
-> Environment variables are case-sensitive.
-> Environment variable names are usually capitalized, but it's not a strict rule.
-> Shell variables are different from environment variables. Shell variables only apply to the current shell and not to any child processes. We'll discuss this further in the section on shell variables vs. environment variables.
Now that we understand these basics, let's move on to the tutorial on setting up Linux environment variables.
1. How to see a list of Linux environment variables?
Now that we understand what Linux environment variables are and their purpose, and we have covered the requirements for setting and viewing Linux environment variables, let's explore some methods to help you get started.
Using printenv to see Linux Environment Variables
The printenv command shows the current environment variables.
To see all your current Linux environment variables, simply run the printenv command without any options or parameters.
# printenv
The output will look similar to this:
You can also use printenv to display individual environment variables. The basic syntax for displaying a single Linux environment variable with printenv is: printenv [name]
For example, to view the PWD environment variable, use this command:
# printenv PWD
The output will display the current working directory of your shell.
Displaying the PWD environment variable
Using env to see Linux Environment Variables
The env command can list all environment variables, similar to printenv. To view all your Linux environment variables using env, run the command without any options or parameters, like this:
# env
In most cases, the output of env should match the output of printenv, except for the _= variable. This occurs because the _= variable is a special Bash parameter used for invoking a shell or shell script.
If you use env on a modern Linux system, the _= value will match the location of the env binary (e.g., /usr/bin/env). If you use printenv, it will match the location of the printenv binary (e.g., /usr/bin/printenv).
Using echo to Display a Linux Environment Variable
One of the quickest ways to display a single Linux environment variable is to use the echo command. The basic syntax is echo $<variable_name>.
For example, to view the PATH environment variable, use this command:
# echo $PATH
The output will be similar to:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The echo $<variable_name> command also works for shell variables that are NOT environment variables.
For example, if we create a BREAKFAST variable and set it to 'Pepper & Egg', we can view the output with echo $BREAKFAST but not with printenv.
Other Methods to See Linux Environment Variables
Other methods for viewing Linux environment variables include:
Using the "declare" command: This built-in shell utility can display shell and environment variables using the "declare" command.
Using the "set" command: This command displays shell variables, environment variables, and shell functions.
Using "compgen -v": This command displays the names of all your shell and environment variables.
2. How to set Linux Environment Variables
The basic command to set a Linux environment variable is:
# export <NAME>='<value>'
For example, to create an environment variable named SANDWICH with a value of Pepper And Egg, use this command:
# export SANDWICH='Pepper And Egg'
To confirm that the environment variable was created, use the command:
# printenv SANDWICH
The output should be as follows:
How to Create a Shell Variable
You can create shell variables using this basic command structure: <NAME>='<value>'.
For example, to create a shell variable named COOLSERVER with a value of Cherry, use this command:
# COOLSERVER='Cherry'
You can confirm the variable now exists using this command:
# echo $COOLSERVER
The output should be:
Cherry
To confirm that the variable is not an environment variable, execute this command:
# printenv COOLSERVER
Because the variable is not an environment variable, there should be no output.
How to Convert a Shell Variable to an Environment Variable
To convert a shell variable to an environment variable, the basic command syntax is: export <Shell_Variable_Name>.
For example, to convert the COOLSERVER variable from the previous example to an environment variable, use this command:
# export COOLSERVER
Now, the printenv COOLSERVER command should print Cherry as the output.
Linux Shell Variables vs. Linux Environment Variables
From what we have learned so far, we can see that shell variables and environment variables are different. Now, let's take a closer look at the difference and why it matters.
The key difference between Linux shell variables and Linux environment variables is that shell variables are not shared with a shell's child processes, while environment variables are shared.
To demonstrate why this difference is important, let's go through an example with a simple bash script.
First, create a variable named CLOUD and set its value to Cherry Servers!.
# CLOUD='Cherry Servers!'
Next, use the echo command to display the variable.
# echo $CLOUD
Here's what it looks like:
Now, create a test.sh bash script that includes the same command. The script should look like this:
#!/bin/bash
echo $CLOUD
Make the script executable with this command:
# chmod +x test.sh
Now, run the script:
# ./test.sh
The output should be blank:
The reason the output is blank is because $CLOUD is a shell variable and it is contained within the current shell instance. However, when the test.sh script is executed as a new process, the $CLOUD environment variable is not set, so the script doesn't print anything.
Let's make $CLOUD an environment variable with this command (note: do NOT use the $ before the variable name):
# export CLOUD
Now, run the test.sh script again.
# ./test.sh
3. How to Remove Linux Environment Variables
To delete an existing Linux environment variable, you can use the command unset <NAME>.
For example, to remove our COOLSERVER variable, use this command:
# unset COOLSERVER
To confirm that the variable is now deleted, execute this command:
# printenv COOLSERVER
Since the variable is no longer an environment variable, there should be no output.
4. How to Make Linux Environment Variables Permanent
The environment variables we have created so far will only exist during your current shell session. If you log out or restart your computer, you would need to recreate them if you want to use them again.
Thankfully, there are different ways to make Linux environment variables persist across multiple shell sessions.
Using .profile to Make Environment Variables Permanent for Login Shells
The .profile file is located in a user's home directory. You can add environment variables for a user by editing their .profile file and including export commands to set the variables.
For example, to make our COOLSERVER environment variable permanent for the current user, follow these steps:
Step 1: Change directory to the user's home directory: cd ~
Step 2: Open the .profile file in a text editor (e.g., nano, vi, vim, etc.).
Step 3: Add the following line at the bottom of the file: export COOLSERVER='Cherry'
Step 4: Save the changes.
Step 5: The changes will take effect upon the next login. You can apply them immediately for the current terminal session using this command:
# . ~/.profile
Using .bashrc to Make Environment Variables Permanent for Non-login Interactive Shells
For non-login interactive shells, you can follow a similar process as with login shells, but instead of editing the ~/.profile file, edit the ~/.bashrc file.
Using /etc/environment to Make Environment Variables Permanent System-wide
Warning: Be cautious when setting system-wide variables as any shell on the system can access them.
To set system-wide Linux environment variables, you can edit the /etc/environment file. Instead of adding export commands to the file, simply add the <NAME>='<VALUE>' pairs at the end of the file.
For example, to make our COOLSERVER environment variable permanent for the entire system, follow these steps:
Step 1: Change directory to the /etc directory: cd /etc
Step 2: Open the environment file in a text editor (e.g., nano, vi, vim, etc.).
Step 3: Add the following line at the bottom of the file: COOLSERVER='Cherry'
Step 4: Save the changes.
That's all.