How to Set and List Linux Environment Variables

Linux environment variables are key-value pairs defining your system’s shell session and program behavior. They let system administrators easily share and distinguish configurations between applications and users.

Admins can modify them using Linux commands to tweak their server settings based on the hosted applications’ needs. Depending on the tasks, you can also set up temporary or permanent environment variables.

To help you understand them, this article will explain Linux environment variables and how to manage them. We will also explore their benefits and best practices for configuring them for efficient system administration.


Environment variables are dynamic values determining the behavior of shells and processes in a Linux system. Using them, admins can easily set default settings and share configurations between applications for more efficient management.
Users can view all the environment variables with their values using the printenv command and set new ones with the export utility.

Linux distributions have environment and shell variables, which function similarly but have different scopes.

Environment variables have a global scope, accessible in all shell sessions, child processes, and other programs. They may apply to a specific user or all accounts within a system.

Meanwhile, shell or local environment variables only apply to the current session in which they are created. You can’t use them on sub-shells, other processes, or different accounts.

How to Manage Linux Environment Variables

In this section, we will explain how to check environment variables in Linux. For a remote system like a virtual private server (VPS), connect using an SSH client like PuTTy or Terminal.

Pro Tip

Hostinger offers a Browser terminal feature for executing commands and managing system variables in Linux servers via web browsers. In addition to convenience, this tool is useful when users disable SSH for security reasons.

How to List Environment Variables in Linux

To list all environment variables in Linux, run the printenv command without any options or flags:

printenv
The printenv command lists all system environment variables

By default, Terminal will print all the environment variables and their values, making the output very long. Add the less modifier to simply the list:

printenv | less

You can also use env, which works similarly to printenv. Meanwhile, the set command prints shell and environment variables.

If you want to show only Linux shell variables of the current user, use grep to filter the output.

In popular Linux distributions, some of the most common environment variables include:

  • PWD – the current working directory.
  • USER – the currently logged-in user account.
  • SHELL – the current user’s shell location.
  • HOME – the user’s home directory.
  • EDITOR – the system’s default file editor.
  • PATH – the location of all system executable files.

These special variables have pre-defined purposes and roles. However, you can set user-defined ones to configure your system based on your needs.

How to Print Environment Variables in Linux

Use the echo command to print a single environment variable value. Here’s the syntax:

echo $VARIABLE

Consider the capitalization since this command is case-sensitive. For example, enter the following to retrieve the PATH environment variable value:

echo $PATH
The echo command prints the current value of the PATH variable

Alternatively, use the printenv command with the variable you want to look up. For instance, run the following to check the HOME environment variable value:

printenv HOME

You can also apply the grep command to check if an entry exists. For example, run the command below to search the USER variable:

printenv | grep USER

To query multiple environment variables’ values, list them after the printenv command like:

printenv VAR1 VAR2

How to Set Environment Variables in Linux

Use the export command to temporarily set a global environment variable in a Linux operating system. Here’s the syntax:

export VAR="value"

Replace VAR with the variable name you want to modify and value with its new value. For example, run the following command to set a new user’s home directory:

export HOME="/etc/another/path"

The output should change if you print the HOME variable value using the echo command. You can also set multiple values using a semicolon like the following:

export VAR="value1:value2"
The export command sets two values for an environment variable

Remember, this command only applies the new global variable for the current environment. Moreover, the changes will reset after you reboot the system.

To set a local environment variable, specify the value directly into the command-line interface using this syntax:

var="value"

Use all lowercase letters to distinguish it from the global variable and avoid overlaps. If you enter a sub-shell using the bash command, printing the value will give an empty output.

Pro Tip

We recommend using the temporary environment variables for tasks like debugging or testing since it won’t affect other shell processes.

How to Make Persistent Environment Variables in Linux

Modify the shell-specific configuration files to make your environment variables persistent. However, the file to edit depends on whether you want to change user-specific or system-wide configuration.

User-specific Environment Variables

Edit the ~/.bashrc to only set a permanent environment variable for the current user. To do so, open it using a text editor like nano:

sudo nano ~/.bashrc

Add the key-value pair at the beginning or bottom of the file for better organization. Here’s the syntax:

export VAR="VALUE"
New environment variables in the bashrc file

Press Ctrl+X, Y, and Enter to close nano and save the changes. Refresh the current shell session to apply the new configuration using this command:

source ~/.bashrc

Instead of ~/.bashrc, modify the ~/.profile file if you want to set variables for login shells.

Note that you can also modify the shell functions through the ~./bashrc file.

System-wide Environment Variables

To set permanent environment variables that apply to all users within an operating system, edit the /etc/environment file using a text editor:

sudo nano /etc/environment

Add the key-value pairs to the file using the basic syntax without export:

VAR="VALUE"
The new environment variable in the environment configuration file

Save the file and close nano. To apply the changes, re-login your current user or restart the system using the reboot command. The configuration will load whenever you enter the bash login shell

In addition to the /etc/environment file, you can add the environment variable to the /etc/profile.d directory. To do so, navigate to the folder using this command:

sudo cd /etc/profile.d

Create a new shell script file to store your new global variable using the nano text editor:

sudo nano global_env.sh

Replace global_env with your desired name, preferably a descriptive one, to simplify identification. In nano, write your environment variables using this syntax:

export VAR="VALUE"

Save the changes and close the text editor. Reboot your system to apply the settings to the bash shell.

If you run the printenv command, the new configuration should show in your Linux environment variables list.

How to Delete Environment Variables in Linux

Use the unset command to remove an environment variable in Linux. Here’s the syntax:

unset VAR

Replace VAR with the variable you want to delete. Remember that the removal only applies temporarily, meaning the key-value pairs will return after the system reboots.

To permanently remove an environment variable, open your ~/.bashrc or /etc/environment configuration file using a text editor. Remove the corresponding line and save the changes.

A hash symbol disabling an environment variable in the environment configuration file

Instead of deleting, we recommend adding a hash symbol (#) at the beginning of the string to deactivate it. You can quickly reactivate the environment variable by removing the character when the change causes an error.

Linux Environment Variables in Development

Environment variables let you easily set up different areas than the code bases to store sensitive information for your applications. It helps improve security since administrators can limit their access to specific users.

In addition, using environment variables in shell scripts helps simplify the automatic deployment process. Instead of manually writing information like API URLs in each .sh file, simply export them to apply the value.

Moreover, it simplifies application management and development since you don’t need to modify the code base frequently. For example, we set two different API URLs as environment variables:

export PYAPP_ENV="TEST"

export PYAPP_TEST_API="https://api.domain.tld/"

export PYAPP_DEPLOY_API="https://api2.domain.tld/"

We call the PYAPP_ENV environmental variable into the application’s code base. During the deployment, we can change its value temporarily into DEPLOY, applying the PYAPP_DEPLOY_API.

Having several variables saves time, as you don’t need to update them individually when moving the application to another development stage.

Linux Environment Variables Best Practices

In this section, we will explore the best Linux environment variable practices to help you manage your server easily and securely.

Use Kodee for Easy Environment Variables Management

All Hostinger VPS hosting plans include Kodee, an AI assistant that lets you quickly generate commands or instructions using simple prompts.

To access the tool, open hPanel and click VPS on the top menu. Select the corresponding server and click Kodee from the sidebar.

It will write the configurations for setting Linux environment variables, which you can quickly copy-paste to the Browser terminal.

For example, ask Kodee, “I need to generate a script to set environment variables for my project. I need three variables: ‘API_KEY’ with ‘123’ value, ‘DB_URL’ with ‘https://example.tld/database’ value, and ‘LOG_LEVEL’ with ‘debug’ value. Write the Bash script for me.”

Hostinger VPS AI Assistant generates script for setting up environment variables

You can also use the tool for Linux variable troubleshooting. For instance, ask Kodee, “I need to change the global environment variables set for all users. I’ve added the new file to /etc/environment, but it doesn’t work. What are the possible causes and their solutions?”

However, we recommend double-checking the generated script or configuration in the development area. As with any AI tool, there might be some inaccurate information.

Use Descriptive Environment Variable Names

Use a descriptive environment variable name to simplify identification and organization. Avoid vague phrases like VAR1 or DATABASE_VAR, as they may complicate management tasks.

You might be unable to identify these variables and their functions, especially if you have multiple environments. In addition, changing the wrong settings can affect your system functionality.

We recommend using a name suggesting the variable’s purpose. For example, if it determines the default file storage directory path, use names like LOG_PATH or EXECUTION_PATH instead of PATH only.

Avoid Variable Scope

Setting the right scope of environment variables in Linux is crucial to ensure system functionality and security. For instance, use a shell variable for temporary tasks, like application deployment target directory and a remote server address.

Meanwhile, set environment variables to define settings multiple applications use, like the HTTP proxy or locale settings.

Setting the wrong variable scope might override the configuration, resulting in an error. Creating a shell variable with the same name as a global one confuses your system since it doesn’t know which one to use.

To avoid issues, set shell variables temporarily so they won’t overlap when used in another session. Also, follow the Linux variable naming conventions to enable the system to distinguish between the global and local ones.

Use all lowercase letters to write a shell variable and uppercase for the global one. For example, use the PATH variable for system-wide tasks and path_deploy_pyapp for temporary use.

Regularly Review and Audit Environment Variables

Regularly auditing Linux variables lets you quickly catch outdated configurations that can cause server malfunctions. Updating ensures they adhere to the latest software and system configurations to help avoid incompatibility issues.

Moreover, they might contain sensitive information like deprecated API keys or credentials, compromising your VPS safety. Regular audits help improve environment variable security, preventing unauthorized parties from accessing them.

To simplify the audit process, create configuration documentation. The simplest way to do so is by exporting variables in Linux using the operator like the following:

set > user_shell_ddmmyyyy.txt

In addition, the command also lets you create Linux environment variable backups for simpler restoration.

Conclusion

Environment variables in Linux are key-value pairs determining your system’s shell and process behavior. They let server administrators easily distinguish and share configurations between users or applications.

To print all environment variables, enter the printenv or env command in Terminal. Meanwhile, run the echo $var utility or use printenv var to check a particular key-value pair.

Use the export command to set new environment variables temporarily. To create a permanent one, add the key-value pairs in the ~/.bashrc or /etc/environment configuration files using a text editor like nano.&nbs

Add a hash symbol (#) at the beginning of the key-value pair in those files to disable the corresponding environment variables. Alternatively, use the unset command to remove them temporarily.

When configuring environment variables in Linux, leverage Hostinger’s AI assistant, Kodee, to simplify the task. In addition, use descriptive names, avoid variable scope, and regularly review your configuration to ensure optimal server performance and security.

Linux Environment Variables FAQ

In this section, we will answer several common questions about Linux environment variables to help you understand them better.

How Do I Find Linux Environment Variables?

To list current environment variables set on your system, use the printenv or env command. Pipe the less flag to print a shorter output.
Use echo $var or printenv var command to check a specific one. Meanwhile, run set to check both environment and shell variables.

What Are the Types of Environment Variables in Linux?

There are local and global environment variables. Local or shell variables apply to the current session in which they are defined and reset upon system reboots.
Global environmental variables have two types. You can use the system-wide configuration using any account, while the user-specific variable applies to one user.

What Is the Difference Between Local and Global Linux Environment Variables?

Local variables apply only to a specific process or the current shell instance in which they are defined. They are inaccessible to other programs or scripts.
Meanwhile, the global ones affect all processes, subprocesses, and shells within an environment. It is accessible to all programs of the same user.

Author
The author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.