Explanation of Environment Variables in Linux
If you’re new to Linux, environment variables might seem like a strange concept, but they’re actually quite simple. Environment variables are variables that are set within the operating system, and are used by applications to determine various settings. For example, an environment variable might be used to define the location of a particular application or library, or to set a default value for a configuration file.
Setting environment variables is an important part of working with Linux, and it’s something that you’ll need to do quite frequently if you’re a system administrator or developer. In this article, we’ll cover everything you need to know about setting environment variables in Linux, from setting variables for a single user to making them persistent across multiple sessions.
Setting Environment Variables for Single and Multiple Users
There are different ways to set environment variables in Linux, depending on whether you want to set them for a single user or multiple users. Here are some methods you can use:
Using the Export Command to Set a Temporary Environment Variable
You can use the export command to set a temporary environment variable in Linux. For example, if you want to set the JAVA_HOME
variable to /usr/lib/jvm/java-11-openjdk-amd64
for the current session, you can run the following command:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Keep in mind that this variable will only be set for the current session, and will not persist after you close the terminal.
Setting Environment Variables for a Single User
To set environment variables for a single user, you can add them to the .bashrc
file in the user’s home directory. This file is executed whenever the user logs in, so any variables you set there will be available for the duration of the session. Here’s an example:
- Open a terminal window and navigate to the user’s home directory:
cd ~
- Open the
.bashrc
file in a text editor:
nano .bashrc
- Add your environment variable to the file, for example:
export MY_VARIABLE=”my value”
Save the file and exit the text editor.
To apply the changes, either log out and log back in, or run the following command:
source ~/.bashrc
Setting Environment Variables for Multiple Users
To set environment variables for multiple users, you can add them to the /etc/environment
file. This file is read by the system at login, so any variables you set there will be available to all users. Here’s an example:
- Open a terminal window and navigate to the
/etc
directory:
cd /etc
- Open the
environment
file in a text editor:
sudo nano environment
- Add your environment variable to the file, for example:
MY_VARIABLE=”my value”
Save the file and exit the text editor.
To apply the changes, either log out and log back in, or run the following command:
source /etc/environment
The tutorial at Serverlab.ca covers setting and unsetting environment variables in Linux for single and multiple users, listing all environment variables, making them persistent for a user’s environment, exporting environment variables, and setting permanent global environment variables for all users.
Listing All Environment Variables
Sometimes, it’s useful to see a list of all environment variables that are currently set on your system. This can help you troubleshoot issues or diagnose problems with your applications. Here are some methods you can use to list all environment variables:
Using the env Command
The env
command is used to print out a list of all environment variables on your system. Simply type env
into the terminal and press enter, and you’ll see a list of all the variables that are currently set. Here’s an example:
$ env
USER=johndoe
HOME=/home/johndoe
LANG=en_US.UTF-8
...
Using the printenv Command
The printenv
command is similar to env
, but it only prints out the value of a specific environment variable. To use it, simply type printenv
followed by the name of the variable you want to check. For example:
$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The above command will print out the value of the PATH
variable, which is used to locate executable files in your system.
The tutorial at Linuxize.com provides instructions on how to set and list environment variables in Linux and Unix-based systems using commands such as env
, printenv
, set
, unset
, and export
. It explains the difference between environment and shell variables and provides common environment variables. The article also covers how to make environment variables persistent by defining them in bash configuration files.
Making Environment Variables Persistent
When you set an environment variable in Linux, it’s only available for the current session. If you close your terminal or restart your computer, the variable will be lost. However, you can make environment variables persistent so that they’re available every time you log in.
Using the .bashrc File
To make environment variables persistent for a single user, you can add them to the .bashrc
file in the user’s home directory. This file is executed whenever the user logs in, so any variables you set there will be available for the duration of the session. Here’s an example:
- Open a terminal window and navigate to the user’s home directory:
cd ~
- Open the
.bashrc
file in a text editor:
nano .bashrc
- Add your environment variable to the file, for example:
export MY_VARIABLE=”my value”
Save the file and exit the text editor.
To apply the changes, either log out and log back in, or run the following command:
source ~/.bashrc
Using the /etc/environment File
To make environment variables persistent for all users, you can add them to the /etc/environment
file. This file is read by the system at login, so any variables you set there will be available to all users. Here’s an example:
- Open a terminal window and navigate to the
/etc
directory:
cd /etc
- Open the
environment
file in a text editor:
sudo nano environment
- Add your environment variable to the file, for example:
MY_VARIABLE=”my value”
Save the file and exit the text editor.
To apply the changes, either log out and log back in, or run the following command:
source /etc/environment
Using the /etc/profile.d/ Directory
Another way to make environment variables persistent for all users is to add them to a script in the /etc/profile.d/
directory. This directory contains scripts that are executed whenever a user logs in. Here’s an example:
- Open a terminal window and navigate to the
/etc/profile.d/
directory:
cd /etc/profile.d/
- Create a new script file with the extension
.sh
, for example:
sudo nano myscript.sh
- Add your environment variable to the file, for example:
export MY_VARIABLE=”my value”
Save the file and exit the text editor.
To apply the changes, either log out and log back in, or run the following command:
source /etc/profile.d/myscript.sh
The tutorial at FreeCodeCamp.org explains what environment variables are, how to set them in Linux, list and print them, make them persistent across multiple sessions, and create a global environment variable accessible by all users.
Unsetting Environment Variables
At times, you may need to remove or unset an environment variable. You can do this using the unset
command in Linux. Here are some ways to unset environment variables:
Unsetting a Single Environment Variable
To unset a single environment variable, you can use the unset
command followed by the name of the variable you want to remove. For example:
unset MY_VARIABLE
This command will remove the MY_VARIABLE
environment variable from your system.
Unsetting All Environment Variables
If you want to remove all environment variables, you can use the env -i
command. This command launches a new shell with no environment variables set. Here’s an example:
env -i bash
This command will launch a new bash
shell with no environment variables set.
Removing an Environment Variable from the .bashrc File
If you’ve set an environment variable in the .bashrc
file and want to remove it, you can simply delete the line that sets the variable. For example, if you have the following line in your .bashrc
file:
export MY_VARIABLE="my value"
You can remove the variable by deleting this line from the file.
The article at BuiltIn.com discusses environment variables in Linux, including local and global variables, how to set them up, check and unset them, and provides examples for common use cases. It also suggests resources for learning more about Linux shell commands.
Exporting Environment Variables
When you set an environment variable in Linux, it’s only available to the current shell. If you want to make the variable available to child processes, you need to export it. Here’s how to do it:
Exporting a Single Environment Variable
To export a single environment variable, you can use the export
command followed by the name of the variable you want to export. For example:
export MY_VARIABLE="my value"
This command will make the MY_VARIABLE
environment variable available to any child processes of the current shell.
Exporting All Environment Variables
If you want to export all environment variables, you can use the export -p
command. This command prints out all environment variables in a format that can be used to set them in a new shell. For example:
export -p > env_vars.sh
This command will write all environment variables to a file called env_vars.sh
, which can be sourced in a new shell to set the variables.
Creating a Global Environment Variable
To create a global environment variable that’s available to all users, you can add it to the /etc/environment
file, as described in section 4. Here’s an example:
MY_VARIABLE="my value"
This line will create a global environment variable called MY_VARIABLE
with the value my value
.
The tutorial at ServerLab.ca covers setting and unsetting environment variables in Linux for single and multiple users, listing all environment variables, making them persistent for a user’s environment, exporting environment variables, and setting permanent global environment variables for all users.
Common Environment Variables
There are many environment variables available in Linux that you can use to customize your system or applications. Here are some common environment variables and their uses:
PATH
The PATH
environment variable lists the directories where the system looks for executable files. When you type a command in the terminal, the system checks each directory in the PATH
variable until it finds the executable file for that command. Here’s an example PATH
variable:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
This PATH
variable includes the directories /usr/local/bin
, /usr/bin
, /bin
, /usr/local/games
, and /usr/games
.
HOME
The HOME
environment variable specifies the home directory of the current user. This variable is used by many applications to determine where to save files. For example, if you’re using the nano
text editor and want to save a file, nano
will look for the HOME
variable to determine where to save the file.
USER
The USER
environment variable specifies the username of the current user.
SHELL
The SHELL
environment variable specifies the default shell for the current user. For example, if the SHELL
variable is set to /bin/bash
, that means the default shell for the user is bash
.
EDITOR
The EDITOR
environment variable specifies the default text editor to use when editing files. Many applications use this variable to determine which editor to launch when you want to edit a file.
LANG
The LANG
environment variable specifies the default language used by the system. This variable is used by many applications to determine which language to use for their user interface.
The tutorial at PhoenixNAP.com explains how to manage environment variables in Linux, including setting, viewing, and unsetting them. It covers checking, exporting, and setting variables permanently, as well as providing a list of common environment variables.
Wrapping Up
Congratulations, you now know how to set and manage environment variables in Linux! In this article, we covered:
- What environment variables are
- How to set environment variables in Linux
- How to view and list environment variables
- How to make environment variables persistent
- How to unset environment variables
- How to export environment variables
- Common environment variables and their uses
We hope this article was helpful in expanding your Linux knowledge. If you want to learn more, be sure to check out our other great content on LINUX HOME PAGE.
Thank you for reading!
Q & A
Q. What are environment variables in Linux, and why do I need to set them?
A. Environment variables are values that can be accessed by applications in Linux. They’re used to customize your system and applications.
Q. How do I set environment variables in Linux?
A. You can set environment variables in Linux using the export
command, followed by the name and value of the variable.
Q. What is the difference between shell variables and environment variables?
A. Shell variables are only available in the current shell, while environment variables are available to any child processes of the current shell.
Q. How can I make environment variables persistent in Linux?
A. To make environment variables persistent in Linux, you can define them in configuration files such as .bashrc
or .bash_profile
.
Q. What are some common environment variables in Linux?
A. Common environment variables in Linux include PATH
, HOME
, USER
, SHELL
, EDITOR
, and LANG
.
Q. What if I accidentally set an environment variable incorrectly?
A. You can unset an environment variable in Linux using the unset
command, followed by the name of the variable.