In this guide, you will learn to Set and Unset Environment Variables in Linux with Examples. Environment variables are commonly used within the bash shell. Also, it can be used for handling web app secrets and configuring services. They are also used to set the important directory location for many packages.
There are two types of shell variables:
- Environment variables (GLOBAL)
- Shell and user-defined variables (LOCAL)
Now you can follow the steps below to set environment variables for a single user and globally for all users. Also, you can list all environment variables and learn how to unset them.
Steps To Set and Unset Environment Variables in Linux with Examples
To complete this guide, you must have access to your server as a root or non-root user with sudo privileges. For this purpose, you can visit the Orcacore website and look for the initial server setup guides.
Step 1 – List All Environment Variables in Linux
At this point, you can use the set command to list all the environment variables including shell, environment, and user-defined shell functions. To do this, you can use the command below:
In your output, you will see:
Output BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=(="0") BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=(="2" ="11") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=(="5" ="1" ="16" ="1" ="release" ="x86_64-pc-linux-gnu") BASH_VERSION='5.1.16(1)-release' COLUMNS=80 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus DIRSTACK=() EUID=0 GROUPS=() HISTCONTROL=ignoredups:ignorespace HISTFILE=/root/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 ...
Step 2 – Set an Environment Variable in Linux
Now you can use the export command to set an environment variable. The syntax for setting an environment variable is as follows:
You should give a name and value to your environment variable. For example:
This will set your Java environment variable and the value of it is the path of your Java location. You can verify it by using the echo command:
This will print the directory location of your environment variable.
Step 3 – Unset an Environment Variable in Linux
If you plan to remove the existence of your environment variable, you can unset it. To do this, you can use the following syntax:
For example, you can unset the Java environment variable with the command below:
Step 4 – Set Persistent Environment Variables in Linux
When you set an environment variable with an export command in the shell, it will end when the user’s session ends. So to make the environment variable persistent for the user’s environment, you can use the user bash profile.
For example, we add the export command with our Java environment variable in the file:
When you are done, save and close the file.
Then, source the bash profile file with the command below:
This will make the environment variable persistent.
Step 5 – Set Environment Variables for Globally for All Users in Linux
At this point, you can set the environment variables globally that can be loaded for all the users in the system. All the global settings are stored in the /etc/profile directory. You must create the environment variables under this directory that can be loaded for all users.
For example, we create and open an HTTP_PROXY file for our environment variable:
Add the desired environment variables to it:
export HTTP_PROXY=http://my.proxy:8080 export HTTPS_PROXY=https://my.proxy:8080 export NO_PROXY=localhost,::1,.example.com
When you are done, save and close the file. This HTTP_PROXY environment variable can now be loaded for all users globally.
With this option, you can make your environment variables globally.
At this point, you have learned to set environment variables for a single user and globally for all users with examples. Also, you have learned to list all environment variables and learn how to unset them. Hope you enjoy using it. You me be interested in these articles: