How to List, Set, and Manage Linux Environment Variables

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. 

Items from an article
  • KVM NVMe VPS 2 GB
    • Country: Australia
    • City: Sydney
    • CPU 2 x Epyc Core
    • RAM 2 GB
    • HDD NVMe : 30GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 10/month
    10/month
  • KVM NVMe VPS 4 GB
    • Country: Australia
    • City: Sydney
    • CPU 2 x Epyc Core
    • RAM 4 GB
    • HDD NVMe : 50GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 20/month
    20/month
  • KVM NVMe VPS 8 GB
    • Country: Australia
    • City: Sydney
    • CPU 4 x Epyc Core
    • RAM 8 GB
    • HDD NVMe : 80GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 40/month
    40/month
  • KVM NVMe VPS 16 GB
    • Country: Australia
    • City: Sydney
    • CPU 8 x Epyc Core
    • RAM 16 GB
    • HDD NVMe : 150GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 70/month
    70/month
Blog company: