- How to Set Environment Variables in Linux
- Setting an Environment Variable
- Unsetting an Environment Variable
- Listing All Set Environment Variables
- Persisting Environment Variables for a User
- Export Environment Variable
- Setting Permanent Global Environment Variables for All Users
- Conclusion
- Managing Environment Variables in Linux
- 1. Overview
- 2. Global Environment Variables
- 3. env and printenv
- 4. export
- 5. Conclusion
How to Set Environment Variables in Linux
In this tutorial, you will learn how to set environment variables in Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to list all environment variables and how to unset (clear) existing environment variables.
Environment variables are commonly used within the Bash shell. It is also a common means of configuring services and handling web application secrets.
It is not uncommon for environment specific information, such as endpoints and passwords, for example, to be stored as environment variables on a server. They are also used to set the important directory locations for many popular packages, such as JAVA_HOME for Java.
Setting an Environment Variable
To set an environment variable the export command is used. We give the variable a name, which is what is used to access it in shell scripts and configurations and then a value to hold whatever data is needed in the variable.
For example, to set the environment variable for the home directory of a manual OpenJDK 11 installation, we would use something similar to the following.
export JAVA_HOME=/opt/openjdk11
To output the value of the environment variable from the shell, we use the echo command and prepend the variable’s name with a dollar ($) sign.
And so long as the variable has a value it will be echoed out. If no value is set then an empty line will be displayed instead.
Unsetting an Environment Variable
To unset an environment variable, which removes its existence all together, we use the unset command. Simply replace the environment variable with an empty string will not remove it, and in most cases will likely cause problems with scripts or application expecting a valid value.
To following syntax is used to unset an environment variable
For example, to unset the JAVA_HOME environment variable, we would use the following command.
Listing All Set Environment Variables
To list all environment variables, we simply use the set command without any arguments.
An example of the output would look something similar to the following, which has been truncated for brevity.
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]="0") BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=([0]="2" [1]="8") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="5" [1]="0" [2]="3" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") BASH_VERSION='5.0.3(1)-release' COLUMNS=208 DIRSTACK=() EUID=1000 GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/ubuntu/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/ubuntu HOSTNAME=ubuntu1904 HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=54
Persisting Environment Variables for a User
When an environment variable is set from the shell using the export command, its existence ends when the user’s sessions ends. This is problematic when we need the variable to persist across sessions.
To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.
- Open the current user’s profile into a text editor
export JAVA_HOME=/opt/openjdk11
Adding the environment variable to a user’s bash profile alone will not export it automatically. However, the variable will be exported the next time the user logs in.
To immediately apply all changes to bash_profile, use the source command.
Export Environment Variable
Export is a built-in shell command for Bash that is used to export an environment variable to allow new child processes to inherit it.
To export a environment variable you run the export command while setting the variable.
export MYVAR="my variable value"
We can view a complete list of exported environment variables by running the export command without any arguments.
SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.1pB5Pry8Id/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.48.2
To view all exported variables in the current shell you use the -p flag with export.
Setting Permanent Global Environment Variables for All Users
A permanent environment variable that persists after a reboot can be created by adding it to the default profile. This profile is loaded by all users on the system, including service accounts.
All global profile settings are stored under /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory named /etc/profile.d, where you will find a list of files that are used to set environment variables for the entire system.
- Create a new file under /etc/profile.d to store the global environment variable(s). The name of the should be contextual so others may understand its purpose. For demonstrations, we will create a permanent environment variable for HTTP_PROXY.
sudo touch /etc/profile.d/http_proxy.sh
sudo vi /etc/profile.d/http_proxy.sh
export HTTP_PROXY=http://my.proxy:8080
export HTTPS_PROXY=https://my.proxy:8080
export NO_PROXY=localhost. 1,.example.com
Conclusion
This tutorial covered how to set and unset environment variables for all Linux distributions, from Debian to Red Hat. You also learned how to set environment variables for a single user, as well as all users.
Managing Environment Variables in Linux
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
Each Linux process has its own set of environment variables, which it inherits from its parent. For example, when we execute a command in our shell, the command inherits the environment variables of the shell.
In this tutorial, we’ll take a look at how we can manage these variables using env, printenv, and export.
Although we’ll be using Bash, all examples in this article should also work in other POSIX-compatible shells.
2. Global Environment Variables
When we start a shell, we create our shell with a set of predefined environment variables. We can declare them globally or on a per-user basis.
With Bash, we declare global variables in /etc/profile. But, we recommend declaring them in their own script files and putting those in /etc/profile.d. Bash will automatically pick these up.
Global variables affect all users in the system. Each user can create a set of “global” environment variables for their own by creating ~/.profile in their home directory.
One of the most common environment variables we use on a daily basis is the PATH variable. PATH contains a list of locations for our shell to search for executables. We can add something to our PATH by creating a ~/.profile containing similar information:
Here, we actually redeclare the PATH variable and set its value to the current value of PATH and add /path/to/executable/ to it.
In the next sections, we’ll describe the commands most commonly used to manage environment variables. Let’s start env and printenv.
3. env and printenv
We’ve already mentioned that each process runs in its own environment. We can use env to run a program in a modified environment, by supplying additional environment variables:
env CUSTOM_VAR=42 /path/to/script.sh
In this example, env will execute /path/to/script.sh in an environment that is a copy of our shell environment with CUSTOM_VAR as an added environment variable with value 42.
Actually, env accepts a list of space-separated variables. Therefore, we can add multiple variables:
env VAR_ONE=1 VAR_TWO=2 /path/to/script.sh
We can also unset or remove variables by preceding them with -u:
env -u PATH /path/to/script.sh
This will run our script in a copy of the current environment, but without the PATH variable.
We can even run our script in an empty environment:
Finally, running env without arguments will print a list of all variables in the current environment. We can accomplish the same with printenv:
[[email protected] ~]$ set HOSTNAME=localhost.localdomain TERM=xterm-256color SHELL=/bin/bash HISTSIZE=1000SSH_TTY=/dev/pts/0 USER=vagrant MAIL=/var/spool/mail/vagrant PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin PWD=/home/vagrant LANG=en_US.UTF-8 HOME=/home/vagrant LOGNAME=vagrant
We can also print values of particular variables by providing a space-separated list of variable names:
printenv HOSTNAME PATH HOME
localhost.localdomain /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin /home/vagrant
4. export
We should not confuse environment variables with shell variables. Shell variables are variables that apply to our current shell only and are not inherited by any programs or scripts that we execute.
We can get an overview of all variables that apply to our current shell by calling set:
[[email protected] ~]$ set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() HOSTNAME=localhost.localdomain TERM=xterm-256color SHELL=/bin/bash HISTSIZE=1000SSH_TTY=/dev/pts/0 USER=vagrant MAIL=/var/spool/mail/vagrant PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin PWD=/home/vagrant LANG=en_US.UTF-8 HOME=/home/vagrant LOGNAME=vagrant
We can see shell variables specific to Bash and all environment variables we printed before using printenv.
Shell variables aren’t inherited by subprocesses, but we can export them to make them visible. We can best explain this by an example.
First, let’s create a new shell variable:
We can print the value of our variable with echo:
echo "Custom var value: $CUSTOM_VAR" 42
To verify that the variable can’t be seen from a subprocess, let’s put the echo command in a script that we’ll name print_custom_var.sh:
#!/bin/bash echo "Custom var value: $CUSTOM_VAR"
When we run the script the output will be:
./print_custom_var.sh Custom var value:
Our script can’t see the value of CUSTOM_VAR, because the CUSTOM_VAR doesn’t exist for the script. We can make it visible to subprocesses with export:
Now, let’s run the script again:
./print_custom_var.sh Custom var value: 42
5. Conclusion
In this article, we learned how we can manage environment variables.
First, we learned how to set global environment variables using locations like /etc/profile.d and ~/.profile. Then, we saw how to manage environment variables using env, printenv, and export.