![Linux Set Environment Variables](https://www.unixmen.com/wp-content/uploads/2024/08/set-environment-variables.png)
In Linux, set environment variables to define system behaviour. Ensure that all applications work as they are intended to. Environment variables in Linux are used to keep configuration settings such as user preferences and file paths. They are important for the optimal functioning of programs and scripts. In this detailed guide, you will learn in Linux how to set environment variables, their importance in the smooth functioning of Linux systems, and some practical examples.
What are Environment Variables in Linux?
As usual at Unixmen, before we learn a concept in detail, we start with the basics. Environment variables are dynamic variables that ensure and determine if the programs and processes running and run the computer work as intended. Environment variables exist in every operating system. They serve as a bridge to convey all the configuration data to programs and processes. Some common examples of environment variables in Linux are “USER”, “HOME”, and “PATH”.
How to View Current Environment Variables in Linux
Before we learn in Linux how to set environment variables, let us see how to view the existing current environment variables. View all the environment variables set in your shell at any point of time by executing the “env” command. Here is the basic syntax:
You can use the “echo” command to view the value of a specific environment variable. Here is the basic syntax for the same:
Linux: Set Environment Variables Temporarily
You can use the “export” command to set the environment variable temporarily. It is important to note that this variable will only be available in the shell session in use. Here is a basic example command in Linux to set environment variables temporarily.
To verify if the environment variable is set in Linux successfully, you can use the “echo” command. Here is an example.
Linux: Set Environment Variables Permanently
You have to add the environment variable to one of the shell configuration files to set the environment variable permanently. The file can be chosen depending on the scope of the variable you would like to have.
Set Environment Variable for a Single User
For a variable that has to be available only to a predetermined user, you can add it to the following files.
- .bashrc
- .bash_profile
- .profile
Open the file in any text editor.
Next step is to add the export command to the file.
Then the final step is to save the file and load the changes:
How to Set Environment Variable for All Users
Add the variable to the “/etc/environment” file to set it globally. Open the file with superuser privileges.
Then, you can now add the variable without the
The final step is to now save the file and reboot the system or use
How to Unset an Environment Variable
You can use the “unset” command to unset the environment variable.
Why it is Important in Linux to Set Environment Variables
- Security: The primary reason is the security benefits. Confidential and sensitive information like API keys and passwords can be stored in environment variables instead of hardcoding them in scripts or applications. This adds an extra layer of security and eliminates the risk of exposing confidential data.
- Efficiency: Setting Environment Variables in Linux makes it faster to set up your environment quickly. This comes very handy when you are working on CI/CD pipelines and automated deployments.
- Flexibility: Environment variables can easily adapt to different environments and configurations. This becomes very helpful as you can write scripts and applications that become more flexible and portable by setting Linux environment variables.
- Configuration management: Environment variables give you the added capability to configure application behaviour without making changes to the code.
Wrapping Up
In Linux, set environment variables to gain faster environment setup times. It is a basic skill set required to become a talented system administrator or developer working on Linux machines. Be it configuring application settings, managing credentials, or optimizing workflow, understanding environment variables is a very essential skillset.
Related Link
RedHat’s documentation on environment variables