How to List, Set, and Manage Linux Environment Variables

Published on September 10, 2024, Updated on December 17, 2024

Linux, renowned for its robustness and flexibility, is a popular choice among developers and system administrators. One of its powerful features is the ability to manage environment variables, which are dynamic values that provide essential configuration information for various processes and applications. 

This guide will delve into how to list, set and manage Linux environment variables and their differences with shell variables.

UNDERSTANDING ENVIRONMENT VARIABLES IN LINUX

Environment variables are key-value pairs that hold information influencing how processes and programs behave within a Linux system. These variables are accessible to all processes running on a system, including the child processes of the shell. They are designed for broader system use.

Here are some of the key points to remember when working with Linux environment variables:

  • Environment variables adhere to the format <NAME>=<VALUE>
  • The names of these variables are case-sensitive. While there's a convention to capitalize their names, it's not a strict rule.
  • It's possible to assign multiple values to a single variable by using colons for separation, like this: <NAME>=<VALUE1>:<VALUE2>:<VALUE3>.
  • Shell variables differ from environment variables in scope. They exclusively affect the current shell session and have no impact on child processes.

ENVIRONMENT VARIABLES VS. SHELL VARIABLES: WHAT’S THE DIFFERENCE?

While both are integral concepts in Linux operating systems, they have distinct characteristics that differentiate them. 

Environmental variables are inherited by child processes from the parent process. They also have a global scope and are not limited to a specific shell session. In addition, they are commonly used to store system-wide configuration settings, paths, and values needed by various processes and applications.

On the other hand, shell variables are specific to the shell session in which they are defined. They also have a local scope; therefore, they are not automatically inherited by child processes. Because of this, they are useful for storing temporary data within a specific shell session, such as user inputs or intermediate calculations.

HOW TO LIST AND SET YOUR LINUX ENVIRONMENT VARIABLES

There are several commands that you need to know to help you list and set environment variables in Linux:

  • env: This command is used when you want to run a new program in a customized environment leaving the current one untouched. You can use it together with an argument to print out the list of the current roster of ongoing environment variables.
  • set: This command can be used to define or assign shell variables. Using it without an argument, you can print a list of all environment and shell variables. 
  • printenv: Users can print specified or all environment variables.
  • unset: You can delete a specific shell or environment variable.
  • export: This command is used to set environment variable.

LISTING LINUX ENVIRONMENT VARIABLES

USING PRINTENV

To list the environment variables available in your Linux system, you can use the printenv command. Execute this command in your terminal: 

printenv

This command will display an extensive list of environment variables and their respective values. If you're looking to view specific environment variables in Linux, you can use this basic syntax as demonstrated below:

printenv [name]

The output will display the current shell working directory.

USING ENV

Alternatively, you can list Linux environment variables using the “env: command. To use env, run the command like this:

env

The env output mostly matches the printenv one except for the _= variable. 

SETTING LINUX ENVIRONMENT VARIABLES

Here’s how to set environment variables in Linux. To set a global environment variable, you can add the definition to your shell's profile using this basic command:

export <NAME>='<value>'

For instance, to create an environment variable under the name PASTA with a value of Tomato and Egg, use this command:

export PASTA='Tomato And Egg'

You can also confirm that the environment variable was created using the command:

printenv PASTA

CREATING A SHELL VARIABLE

You can easily create shell variables by running this command

<NAME>='<value>.'

For instance, if you create a shell variable called FLEET, you can confirm that it's not a Linux environment variable by using this command,

printenv FLEET.

CONVERTING YOUR SHELL VARIABLE TO AN ENVIRONMENT VARIABLE

If you want to convert your shell variables to Linux environment variables, use this command:

export <Shell_Variable_Name>

MANAGING LINUX ENVIRONMENT VARIABLES

As your system grows in complexity, so does the need to manage environment variables efficiently. Let’s look at some of the ways to manage them:

HOW TO MAKE LINUX ENVIRONMENT VARIABLES PERMANENT

The Linux environmental variables you create will only be available during the course of your shell session. To ensure that your variables are available every time you log in, you can export them in your shell profile configuration file. Here’s how to go about it:

USE .PROFILE

You can access this file from the home directory. To add Linux environment variables for a particular user, you can manipulate the .profile file and add export commands. 

Once you are done with the amendments, save them to effect them in the next login. To effect the changes in the current terminal, use this command: 

~/.profile

USE .BASHRC 

Alternatively, you can make the Linux environment variables permanent by using the .bashrc command. This is useful for non-login shells. Follow the same process above, but instead of editing the ~/.profile file, you should edit the following:

~/.bashrc

USE /ETC/ENVIRONMENT

This file has all the system-wide Linux environment variables. You can add new variables by editing the /etc/environment file. For this, instead of appending export commands, edit the

<NAME>='<VALUE>' pair to the end of the file.

HOW TO DELETE LINUX ENVIRONMENT VARIABLES

If you want to delete the Linux environment variables, use this command:

unset <NAME>

To confirm that the variable has been deleted, run this command:

printenv [Name of variable]

BEST PRACTICES TO LIST, SET, AND MANAGE LINUX ENVIRONMENT VARIABLES

To maximize the benefits of environment variable management, consider these best practices:

  • Use descriptive names: Ensure that you use descriptive names for your variables to enhance readability and understanding.
  • Maintain documentation: This documentation details the purpose and usage of each environment variable. This will prove invaluable when collaborating with others or revisiting your projects.
  • Sensitive Information: Avoid storing sensitive information, such as passwords or API keys, as environment variables in plaintext. Instead, use dedicated secret management tools.
  • Regular Review: Periodically review and clean up unnecessary or obsolete environment variables to keep your system organized and efficient.

CONCLUSION

Linux environment variables play a crucial role in configuring processes, applications, and system behavior. By understanding how to list, set, and manage these variables effectively, you can optimize your system's performance and maintain a well-organized development environment. Follow this guide to enhance your proficiency in Linux environment variables.