How to Set Environment Variables in Linux?
Introduction
An environment variable is like a magic wand that can give information on the location of specific directories, passwords, and API keys. This system variable has vast possibilities in terms of software and application development. Do you know this variable can be configured differently on each system? Yes, every operating system has its own set of variables that can be configured as per the requirement.
This article will focus mainly on how to set environment variables in Linux. Moreover, you will get a wide-angle view of the most common examples relating to this exceptional setting. Let’s start by reading about the basic concept of this variable.
Understanding Environment Variables
Environment variables make application deployment easier in different environments. Also, it is easy to change the environment variable runtime, which leads to dynamic applications and programs. This system variable offers the following functionalities:
- Configuration: Helps customize application behavior.
- Security: Responsibly stores sensitive information with high-end security.
- Inter-process communication: Let the programs easily share data between processes.
- Platform Independence: Gives support for portable applications across systems.
- Dynamic Behavior: Allows for runtime changes to pave the way for dynamic application.
- Ease of Deployment: Environment variables lead to simple deployment of applications.
Each operating system has its own set of variables, and you can also set environment variables in Linux. Having the right system settings or commands will help you place the proper variable. Now, let’s move on to some common examples of these specified variables in Linux.
Common Examples of Environment Variables in Linux
There are different ways to use environment variables in Linux, and we have listed the most common ones for your convenience. Go through this quick list of examples.
- PATH: Every computer has an executable program that can be run with just a simple click. The “PATH” variable shows the location of these executable programs.
- HOME: Many files and configurations are stored in the home directory. The way to this directory is easily accessible through the “HOME” variable.
- USER: This helps in storing the credentials of the current user about the system.
- LANG: If you need to find the default, then simply use the “LANG” variable.
- DISPLAY: Everything on the computer screen is controlled through the server manager. A “DISPLAY” variable specifies this server manager for the client.
- EDITOR: Linux uses a command line interface that works with a text editor. The “EDITOR” variable specifies the default text editor to be used.
- TZ: This variable helps set the timezone for programs.
How to Check the Environment Variable?
Checking the environment variable in Linux is helpful during the setting up process. The user gets the details of the instructions attached to the variable during the checking process. If there is an anomaly or requirement for a change in the program behavior, then the user must check the environment variable. Let’s take a look at how to get through this process.
- Using the echo command: Use the following command to print the value of the “PATH”:
- Using the printenv command: You can print all the environment variables using the command below.
If there is any need to print a specific variable, then simply use the following command and specify the name of the variable in the syntax:
Set Environment Variables in Linux
There are two methods for setting environment variables in Linux: temporary and permanent. The content below will look at each category in detail.
How to set environment variables in Linux Temporarily?
The duration between a particular log-in and log-out is called a session. As a user, you can set specific variables to set these sessions and avoid the permanent setting. The “echo” command will be used to perform the function. Take a look at the syntax below.
Here, we have set the value of “MY_VARIABLE” as “example.” The term “value” refers to the content of the variable.
Steps to set permanent environment variables in Linux
There are two different approaches in Linux to set environment variables permanently. Let’s start with the very first method.
- ~/.bash_profile:
Simply add the variable to the “~/.bash_profile” file in the home directory. Use the following command to do so:
Save the file once the command is placed.
- ~/.profile:
If the default shell is not Bash, then simply add the variable in the “~/.profile” file. Use the command below to open the “~/.profile” file through the text editor.
Now use the following command to add the variable to this file.
Replace the term “MY_VARIABLE” with the chosen variable name and the term “example” with the required value. At last, save the changes and exit using the “Ctrl + O” and “Ctrl + X” systematically.
Also Read: DNS Cache Flushing: Troubleshooting Guide for All Operating Systems
Conclusion
The setup of environment variables plays a crucial role in Linux systems. It is important for flexibility and customization for various applications and system settings. The variables serve as dynamic tools that facilitate application deployment, configuration, security, and inter-process communication. Understanding how to set environment variables in Linux, whether temporarily or permanently, empowers users to tailor their system environment to suit specific needs.
You can easily edit the variable on a permanent or temporary basis using methods like “~/.bash_profile” or “~/.profile”. The user can easily enhance the efficiency and functionality of their Linux systems. Whether it’s configuring common variables like PATH or HOME, or defining custom variables for specific applications, the setup of environment variables in Linux is essential for effective system administration and development.