How to Set Environment Variables in Linux
Environment variables are dynamic-named values that can influence the behavior of running processes on a computer. They exist in every computer system and are used to make the system aware of certain conditions. In Linux, environment variables play a crucial role in configuring software applications, storing sensitive information, and setting system-wide preferences. This guide will teach you how to set, list, print, and make environment variables persistent in Linux.
What Are Environment Variables?
Environment variables are name-value pairs used by a system’s shell to define default shell properties. Some of these include your shell’s home directory, prompt, and current working directory. Environment variables are inherited by sub-shells and are available to applications and daemons. You can also create and set your own environment variables.
Environment Variable Scope
A variable’s scope refers to the parts of a program or environment that can access a given variable. An environment variable in Linux can have a global or local scope.
- Globally scoped environment variables are accessible from anywhere in a particular environment bound by the terminal.
- Locally scoped environment variables can only be accessed by the terminal that defines the variable. They cannot be accessed by any program or script.
Differences Between Environment and Shell Variables
Standard UNIX variables are classified into two categories: environment variables and shell variables.
- Environment variables are available and valid system-wide. They can be used by scripts and applications and are inherited by all spawned child processes and shells. By convention, environment variables are given uppercase names.
- Shell variables are available only in the current shell session. They are useful when you need to store values temporarily. Each shell, such as zsh and bash, has its own set of internal shell variables.
Commonly Used (Global) Environment Variables
The following environment variables are commonly available to most popular Linux systems by default:
- USER: The currently logged-in username.
- HOME: The path to the current user’s home directory.
- SHELL: The pathname of the current user’s shell.
- PATH: A list of directories that the shell searches for executable files.
- PWD: The path to your current working directory (PWD stands for “Print Working Directory”).
- UID: The current user’s unique identifier.
How to Set Environment Variables in Linux
Temporary Environment Variables
To set an environment variable temporarily, which will only last for the duration of the shell session, you can use the export
command:
For example, to set the JAVA_HOME
environment variable temporarily:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Persistent Environment Variables for a Single User
To make an environment variable persistent across sessions for a single user, add the export
command to the user’s profile script, such as ~/.bashrc
, ~/.bash_profile
, or ~/.profile
:
echo 'export VAR_NAME="value"' >> ~/.bashrc
For example:
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
After adding the variable, you can apply the changes immediately by sourcing the profile script:
source ~/.bashrc
Persistent Environment Variables for All Users
To set an environment variable globally for all users, you can create a script in the /etc/profile.d/
directory:
sudo echo 'export VAR_NAME="value"' > /etc/profile.d/myenv.sh
For example:
sudo echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' > /etc/profile.d/java.sh
Make sure the script is executable:
sudo chmod +x /etc/profile.d/myenv.sh
How to List Environment Variables in Linux
To list all environment variables, you can use the env
, printenv
, or set
commands:
env
Or
printenv
Or
set
How to Print Environment Variables in Linux
There are two ways to print the already-defined environment variables:
printenv VARIABLE_NAME
echo $varname
For example, to print the value of the SHELL
variable using both methods:
printenv SHELL
And
echo $SHELL
How to Unset Environment Variables
To remove an environment variable, use the unset
command:
unset VAR_NAME
For example:
unset JAVA_HOME
Best Practices for Using Environment Variables
- Use descriptive names for your environment variables, so they clearly indicate their purpose.
- Keep the names and structures of your environment variables consistent across different environments (e.g., development, testing, production) to avoid confusion and potential errors.
- For application-specific variables, using a prefix can be a good practice. This can help you quickly identify the variables related to a specific application and prevent potential clashes with other variables.
- Consider using tools for managing environment variables, such as secret management or configuration management systems.
Conclusion
In this comprehensive guide, you learned how to create, define, list, print, and make environment variables persistent in Linux. You also learned about the different scopes of environment variables and best practices for using them. By understanding and effectively managing environment variables, you can ensure that software applications have the necessary configuration to operate correctly and securely.