How to List and Set Environment Variables in Linux?

Learn via video courses
Topics Covered

Environment variables play a crucial role in the Linux operating system, providing a way to store and retrieve important information that various programs and processes can access. These variables contain data such as system paths, user preferences, and configuration settings. In this article, we will explore how to list, delete and set environment variables in Linux, empowering users to manage their system environment effectively.

What are Environment Variables in Linux?

Environment variables in Linux are dynamic values that hold information used by the operating system and various processes running within it. They consist of name-value pairs, where the name represents the variable's identifier, and the value holds the corresponding data. These variables are stored in the shell's environment and are accessible to all running processes, enabling them to access and utilize the information stored within.

Environment variables play a significant role in configuring the behavior of the Linux system and influencing the behavior of programs and scripts. They provide a way to define system-wide settings, customize user preferences, specify paths for executable files, and control the runtime environment for applications.

Environment variables are commonly used for various purposes in Linux, which include:

  • Configuring system behavior: Variables like PATH, LANG, and LC_* determine the system's language, character encoding, and localization settings.

  • Defining user preferences: Variables like HOME, USER, and SHELL provide information about the current user's home directory, username, and default shell.

  • Specifying search paths: The PATH variable lists directories to search for executable files, allowing users to run commands from anywhere in the system.

  • Storing configuration settings: Many applications and services use environment variables to store their specific configuration options, allowing customization without modifying code or configuration files.

  • Setting runtime options: Environment variables can influence the behavior of programs and scripts by specifying runtime options or parameters.

How to List Linux Environment Variables?

To list the environment variables in Linux, you can use various commands and methods.

Here are three commonly used approaches:

Using the env command

The simplest way to list all the environment variables is by executing the env command in the terminal. Open a terminal window and type env followed by pressing Enter. This command will display a list of all the current environment variables and their corresponding values.

env command

Using the printenv command

Another command that provides similar functionality is printing. It specifically prints the environment variables without any additional information.

printenv command

Using the set command

The set command displays all variables in the shell, including environment variables, shell variables, and shell functions. It provides more detailed information, such as variable types and values.

set command

Note, that the set command will display a large amount of output, including other variables and functions in addition to environment variables.

By using any of these methods, you can obtain a comprehensive list and set environment variables in Linux. This information is useful for understanding the current configuration and can be utilized when setting or modifying environment variables as needed.

How to Set Environment Variables in Linux?

Setting Linux environment variables allows users to define custom variables or modify existing ones to control system behavior and provide data to various processes and applications. Here are a few methods to set environment variables in Linux:

Temporary assignment using the export command:

To set a temporary environment variable for the current session, you can use the export command in the terminal. This method is suitable when you need to set a variable for immediate use within the session.

Syntax:

Example:

temporary assignment using the export command

This command sets the Variable name as the 'Linux OS' environment variable with the variable value which is 'Fedora And CentOS" for the current session. The variable will be accessible to other processes and programs executed within the same session.

Modifying shell-specific configuration files:

Environment variables can be set by modifying configuration files specific to the shell being used. The most common configuration file is ~/.bashrc for the Bash shell. By editing this file, you can set environment variables that persist across sessions.

Open the ~/.bashrc file in a text editor and add the following line at the end:

Save the file and either restart the shell or run the command source ~/.bashrc to apply the changes to the current session. The MY_VARIABLE variable will now be available in subsequent sessions.

Setting variables system-wide:

To set environment variables that are available to all users and all sessions on the system, you can add the variable assignments to a file in the /etc/profile.d/ directory.

Create a new file, such as myenvvars.sh, in the /etc/profile.d/ directory using a text editor. Add the following line to the file:

Save the file and exit. The variable will now be set system-wide and available to all users on the system. By using these methods, you can effectively set environment variables in Linux and customize the system environment according to your requirements.

Note, For these changes to take effect, users may need to log out and log back in or restart the system.

How to Delete Linux Environment Variables?

In Linux, you can delete or unset environment variables to remove them from the current session or shell. Here are two common methods to delete environment variables:

Using the unset command:

The unset command is used to unset or delete environment variables. By specifying the variable name as an argument to the unset command, you can remove it from the current session.

Syntax:

Example:

This command will unset or delete the MY_VARIABLE environment variable, removing it from the current session. The variable will no longer be accessible by other processes or programs within the same session.

Editing shell-specific configuration files:

To delete an environment variable permanently, you can edit the relevant configuration file associated with the shell being used.

For example, if you set the environment variable in the ~/.bashrc file, open the file in a text editor and locate the line where the variable is defined. Remove or comment out the line that sets the variable, save the file, and exit. This change will take effect in subsequent sessions, effectively removing the variable.

If you set the environment variable in a system-wide configuration file located in the /etc/profile.d/ directory, you can edit the corresponding file and remove the line that sets the variable. Save the file and restart the system or log out and log back in for the changes to take effect.

By removing the variable from the configuration files, it will no longer be set in future sessions. Remember that when you delete an environment variable, any programs or scripts that rely on that variable may be affected. Therefore, it's essential to consider the impact before removing variables.

Using these methods, you can easily delete or unset environment variables in Linux, either for the current session or permanently by modifying the relevant configuration files. So that was how to check environment variables in Linux.

Learn More

For further exploration of Linux and related topics, you can refer to the following Scaler Topics articles:

Advanced Commands in Linux

Bash Scripting in Linux

Conclusion

  • Environment variables in Linux are dynamic values that store information used by the operating system and processes running within it.

  • To list Linux environment variables, you can use commands like env, printenv, or set in the terminal. These commands provide different levels of detail about the variables.

  • To set Linux environment variables, there are several methods available. You can use the export command for temporary assignments within the current session, modify shell-specific configuration files like ~/.bashrc for persistent variables, or set variables system-wide by creating files in the /etc/profile.d/ directory.

  • Deleting or unsetting environment variables can be done using the unset command for the current session or by editing the relevant configuration files associated with the shell being used, such as ~/.bashrc or system-wide files in /etc/profile.d/.

  • Understanding how to manage environment variables in Linux empowers users to customize their system environment and control the behavior of processes and applications according to their needs.