Written by Oleksandr Serebro
Lead Writer | Marketing expert
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.
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:
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.
There are several commands that you need to know to help you list and set environment variables in Linux:
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.
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.
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
You can easily create shell variables by running this command
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,
If you want to convert your shell variables to Linux environment variables, use this command:
export <Shell_Variable_Name>
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:
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:
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
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
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
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]
To maximize the benefits of environment variable management, consider these best practices:
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.
Share your thoughts and help us improve! Your feedback matters to us