Linux environment variables are a unique feature of the OS that stores values about the system’s various parameters. Experienced users, in particular, can set a value for these variables to speed up workflows.
By configuring these variables, you can enhance application performance and specify particular applications and parameters for their execution. You can employ these variables to specify initial arguments and parameters for the shell, subshell, and various applications launched via the GUI. Additionally, for tasks like using the unzip command in Linux or changing file permissions in Linux, these variables can play a crucial role in customizing your environment
This tutorial delves into the fundamentals of Linux environment variables, including how to check the Linux version and utilize the Linux pipe command to manipulate these variables. It also guides you on efficiently setting new values for these variables. The beauty of these variables is their universal compatibility, making them applicable to any Linux distribution without concerns about conflicts.
Let’s start with Linux environment variables.
Table Of Content
- Prerequisites to Using Linux Environment Variables
- What Are Linux Environment Variables?
- How the Environment and Environmental Variables Work in Linux
- The Most Common Environment Variables
- How To List Environment Variables In Linux
- How to Set Environment Variables in Linux?
Prerequisites to Using Linux Environment Variables
Here are some prerequisites to using and setting Linux environment variables.
- Shell Basics: Familiarity with a command-line interface (CLI) and shell scripting.
- Commands: You should be comfortable with basic CLI commands like export, echo, and unset.
- Naming Convention: Understand variable naming rules (case sensitivity, no starting numbers).
- Values: Handle string, number, path values, and path variables.
- Paths: Work with file paths, installation paths, and a list of directories.
- Global/Local Variables: Differentiate global and local variables.
- Persistence: Define variables in executable files for persistence.
- Operating System: Have a Linux distro, like Ubuntu, CentOS, Debian, AlmaLinux, or RockyLinux on the system.
What Are Linux Environment Variables?
Environment variables are defined in a particular environment (usually at the OS level).
In Linux distributions, environment variables are frequently employed within the Bash shell, whether you’re comparing Arch Linux vs. Ubuntu or seeking the ideal Linux OS for gaming. These variables, often referred to as global variables, play a crucial role in tailoring the system’s functionality and managing the startup behavior of various applications across the system.
On the other hand, local variables are restricted and accessible from within the shell in which they’re created and initialized.
Generally, Linux environment variables are available globally and system-wide. As such, the subshells and child processes can also access these variables.
Linux environment variables have a key-value pair structure, separated by an equal (=) sign. Note that the names of the variables are case-sensitive and should be in uppercase for instant identification.
Here’s a typical example of a Linux environment variable:
If a variable has multiple values, they are separated by a semicolon:
If you need to include a space in the value of a variable, you should enclose it with quotation marks. However, traditionally, the variable name includes an underscore for better readability:
Now that you understand what Linux environment variables are, here’s a list of the popular environment variables.
How the Environment and Environmental Variables Work in Linux
In the world of Unix and Linux, the environment encompasses variables that define operating conditions for processes and applications. These variables, whether you’re considering Unix vs Linux, play a crucial role in storing information such as system paths, user preferences, and configuration settings. Environmental variables exert significant influence over application behavior and facilitate essential tasks like using the grep command in Linux or passing specific directory paths to text editors
These variables can be categorized into two types: global and local. Global variables, which are accessible system-wide, impact all processes across the system. On the other hand, local variables are confined to a specific process and its offspring, often governed by configurations in the .bashrc file in Linux.
Users can view and modify these variables using commands like ‘export’ and ‘echo.’ Common environmental variables include PATH (specifies directories to search for executable files), HOME (user’s home directory), and LANG (default language settings).
When a process is launched, it inherits its parent’s environment but can modify or add variables as needed. This mechanism enables processes to communicate, share data, and adapt to different runtime conditions in the Linux environment.
The Most Common Environment Variables
Linux environment variables store values that help the system and the applications discover user preferences. Some standard globally available environment variables store necessary user preferences and options information.
PATH: The path environment variable contains a colon-separated list of folders where the system should look for system and user files.
HOME: The location of the logged-in user’s home directory.
SHELL: The location (path) of the shell the logged-in user prefers.
ENV: Contains the list of all environment variables.
UID: Contains the unique identifier of the logged-in user.
TEMP: Contains the path of the folder for temporary files.
This list covers the environment variables that contain essential information about how the current user has set up the system for their use. This includes the location of their files and system files.
How To List Environment Variables In Linux
First, you must know how to find the environment variables and their values. You can opt for several ways to list Linux environment variables for this.
This simple command lists the values of the current environment variables in the terminal. When used as is without any arguments, it lists all the variables.
[root@xxxx ~]# printenv
If you wish to find out the details of a specific variable, you can use the printenv command with the variable’s name as an argument. For instance, if you want to know the value of the HOME variable, you can use the printenv HOME command:
[root@xxxx ~]# printenv HOME
[root@xxxx ~]# printenv HOME MAIL
If you are not sure of the exact name of the variable, you can use grep to find the likely candidates. For instance, you can use this combination to find information about SSH-related variables. Using grep with the printenv command prints the list of all variables containing SSH.
[root@xxxx ~]# printenv |grep SSH
SSH_CLIENT=x.x.x.x 1592 2xx5
SSH_CONNECTION=x.x.x.x 1592 x.x.x.x 2xx5
The env command is often used to quickly check the environment variables set for the current session.
LESSOPEN=| /usr/bin/lesspipe %s
Since env could result in a long list of variables, finding the variables you’re looking for could take time. In this case, you can always use the command with grep to filter down the list to the variables you’re looking for.
Here’s how you can use env with grep to find out the SSH-related variables:
[root@xxxx ~]# env |grep SSH
SSH_CLIENT=x.x.x.x 1592 2xx5
SSH_CONNECTION=x.x.x.x 1592 x.x.x.x 2xx5
The main reason why many power users don’t prefer env or printenv is because of their limited scope, where they print just the environment variables.
On the other hand, the set offers a much broader scope and prints out the complete list of environment and shell variables and functions. As a result, this command can generate a long list spanning multiple screens.
We recommend using the less command to view the output one screen at a time.
# set | less
It is a common command to list the value of shell variables in the terminal. Remember to use the $ sign before the variable’s name.
[root@xxxx ~]# echo $SHELL
This popular utility is used for listing and setting a shell variable. It prints a long list of shell and environment variables when used without arguments. We suggest using the less command to view the list easily.
Now that you know how to check Linux environment variables, let’s go into the details of how to set the Linux environment variables.
How to Set Environment Variables in Linux?
Setting up environment variables is pretty easy. We can use the export command for setting up a new environment variable in the following format.
For example, the following command will set the Java home environment directory.
# export JAVA_HOME=/usr/bin/java
Note that you won’t get any response about the success or failure of the command. As a result, if you want to verify that the variable has been properly set, use the echo command.
The echo command will display the value if the variable has been appropriately set. If the variable has no set value, you might not see anything on the screen.
It’s important to understand that any variable you set with the export command will only remain for the duration of the session. Once the session terminates for any reason, the variables you set will become “unset” and won’t be available in the subsequent sessions.
This is a common issue because users wish to set the variables for a longer term, and the loss of values means that the user has to set these variables at the start of each session.
Let’s see how to make the variables “persist” beyond the current session.
How to Create a Shell Variable
Start with the following steps on how to create a shell variable in Linux:
- Launch your terminal application (usually located in the system’s applications). You can also do a quick search for “Terminal.”
- Decide on a name for your variable. Make sure it follows the variable naming convention and is descriptive so that you can guess the contents. For this example, let’s use system_msg.
- Use the following syntax to assign a value to your variable:
# system_msg="Hello, Redswitches!”
You can replace “Hello, Redswitches!” with the value you want to assign.
- Verify the success of the process by accessing the value of the system_msg variable. We’ll use the echo command followed by the variable name preceded by a dollar sign $:
# echo $system_msg
This will display:
Remember that this variable will only exist within the current shell session. If you want it to persist across sessions, you can add the variable assignment to a shell configuration file like ~/.bashrc.
Make Environment Variables “Persist”
As mentioned earlier, variables you set during a session expire once the session terminates. This often annoys users because they expect variables to be always available after they set these variables.
To make an environment variable persistent for a user’s environment, you must edit the .bashrc file in your favorite editor:
# sudo nano ~/.bashrc
Add an entry for each variable you wish to be always available. Remember to follow the proper syntax to avoid surprises:
# export [VARIABLE_NAME]=[variable_value].
Once done, save and exit the file.
Next, restart the shell so that the latest values for the variables are loaded.
You should know that when a new session starts, the variables can be read from multiple locations (for instance, from /etc/environment and /etc/profile. You can choose the source of the variables with the source command. For instance, if you wish to load variables for the bash shell, use the source command with the following argument.
# source ~/.bashrc
The next step is to verify that the variables are correctly set and available for use. For this, we’ll use the echo command.
# echo $JAVA_HOME
Persisting Global Environment Variables for All Users
To make a permanent environment variable persist after a reboot, add it to the default profile. All users on the system, including the service accounts, load this default profile.
All global profile settings are located under the/etc/profile. While you can edit this file directly, we recommend storing the global environment variables in a directory called /etc/profile.d, where you can find a list of files used to set environment variables for the entire system.
Let’s create a new file under /etc/profile.d to store the global environment variable. The file’s name should be contextual so others can easily understand its purpose.
For example, we will create a permanent environment variable for JAVA_HOME.
# sudo vi /etc/profile.d/java_home.sh
Add the following lines to export the variable:
# export JAVA_HOME=/usr/bin/java
Save your changes and exit.
How to Unset an Environment Variable
To unset an environment variable, we can use the unset command:
# unset [VARIABLE_NAME].
This command permanently removes variables exported through the terminal. Variables stored in configuration files will also be removed from the current shell session. However, the system can fetch these back upon the next login.
To permanently unset a variable you stored in a file, go to the file and remove the entry containing the variable definition.
This tutorial encompassed the establishment and removal of environment variables, a task applicable to all Linux distributions. Additionally, you gained insights into making environment variables persistent for a single user or all users, whether you’re working with a dedicated server, bare metal server, or even a high-performance 10 Gbps dedicated server. These dynamic entities store specific values essential for configuring the system.
Understanding shell environment variables is key to system customization. Whether you require a Miami dedicated server, GPU dedicated server, instant dedicated server, or specialized Linux hosting, RedSwitches, a trusted hosting provider, complements this knowledge with dependable services. Harnessing the power of environment variables alongside robust hosting solutions ensures a smooth Linux experience, a critical factor for achieving success in any digital endeavor.
And, if you’re looking for a robust server for your Linux projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Let us know what Linux environment variables you use and how you set them!
Q: What are environment variables, and how are they used in Linux?
A: Environment variables are dynamic values that are used by the operating system and are accessible to all running processes. They are typically used to store configuration settings or provide program information. In Linux, environment variables are set and accessed through the shell.
Q: How do I set an environment variable in Linux?
A: To set an environment variable in Linux, you can use the export command followed by the variable name and value. For example, to set a variable named “MY_VAR” to the value “hello”, you would use the command export MY_VAR=hello.
Q: How do I unset an environment variable in Linux?
A: To unset an environment variable in Linux, you can use the unset command followed by the variable name. For example, to unset the variable “MY_VAR”, you would use the command unset MY_VAR.
Q: How do I list all the environment variables in Linux?
A: You can use the command to list all the environment variables in Linux. This will display a list of all the variables and their values.
Q: How do I access the value of an environment variable in a shell script?
A: To access the value of an environment variable in a shell script, you can use the $ symbol followed by the variable name. For example, to access the value of the variable “MY_VAR”, you would use $MY_VAR in your script.
Q: How do I make environment variables persistent in Linux?
A: To make environment variables persistent in Linux, you can set them in your shell’s configuration file. For example, if you are using the Bash shell, you can add the export command to the .bashrc or .bash_profile file in your home directory.
Q: How do I print the value of all the environment variables?
A: To print the value of all the environment variables in Linux, you can use the printenv command. This will display a list of all the variables and their values.
Q: How do I set an environment variable to a list of directories?
A: To set an environment variable to a list of directories in Linux, you can separate the directories with a colon (:). For example, to set a variable named “PATH” to a list of directories “/usr/bin” and “/usr/local/bin”, you would use the command export PATH=/usr/bin:/usr/local/bin.
Q: What is the difference between a shell variable and an environment variable in Linux?
A: A shell variable is a variable that is only accessible within the current shell session, while an environment variable is accessible by all running processes. Shell variables are typically used for temporary storage, while environment variables are used for configuration settings and information sharing.
Q: How do I define environment variables in Red Hat Enterprise Linux?
A: To define environment variables in Red Hat Enterprise Linux, you can set them in the /etc/profile file. All login shells source this file and provides a central location to set system-wide environment variables.