How to Set Environment Variables in Linux – Serverlab

In

this tutorial, you will learn how to set environment variables on Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to enumerate 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, you use the export command. We give the variable a name, which is what is used to access it in scripts and shell configurations and then a value to hold the data that is needed in the variable.

export NAME=VALUE For example, to set the environment variable

for the home directory of a manual installation of OpenJDK 11, we would use something similar to the following.

export JAVA_HOME=/opt/openjdk11 To generate the value

of the shell environment variable, we use the echo command and prepend the variable name with a dollar sign ($). echo

$JAVA_HOME

And as long as the variable has a value, it will repeat. If no value is set, an empty line is displayed instead.

To

unset an environment variable

,

which removes its existence entirely, we use the unset command. Simply replacing the environment variable with an empty string won’t delete it, and in most cases will probably cause problems with scripts or the application expecting a valid value.

The following syntax is used to override the setting of an

unset environment variable VARIABLE_NAME For example,

to unset

the environment variable JAVA_HOME, we would use the following command. unset JAVA_HOME List of all set environment variables

To list all environment variables, we simply use the set command without any arguments.

Establish

An example of the output would look somewhat 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

Persistent environment variables for a

user

When you set an environment variable from the shell using the export command, its existence ends when the user’s sessions end. This is problematic when we need the variable to persist in all sessions.

To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.

  1. Open the current user’s profile in a text editorvi ~/.bash_profile
  2. Add the export command for each environment variable that you want to persist.export JAVA_HOME=/opt/openjdk11
  3. Save your changes.

Adding the environment variable to a user’s bash profile alone will not automatically export it. However, the variable will be exported the next time the user logs on.

To immediately apply all changes to bash_profile, use the source command.

Source ~/.bash_profile 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 an environment variable, run the export command

while setting the

variable. export MYVAR=”my variable value”

We can see a complete list of exported environment variables by running the export command without any arguments

. export 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, use the -p flag with export.

export -p

Configuring permanent global environment variables for all users

You can create a permanent environment variable that persists after a restart by adding it to the default profile. This profile is loaded by all users of the system, including service accounts.

All global profile settings are stored in /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory called /etc/profile.d, where you will find a list of files that are used to set environment variables for the entire system.

  1. Create a new file in /etc/profile.d to store the global environment variables. The name of the must be contextual so that others can understand its purpose. For demonstrations, we will create a permanent environment variable
  2. for HTTP_PROXY.sudo touch /etc/profile.d/http_proxy.sh

  3. Open the default profile in a text editor.sudo vi /etc/profile.d/http_proxy.sh
  4. Add new lines to export environment variablesexport HTTP_PROXY=http://my.proxy:8080export HTTPS_PROXY=https://my.proxy:8080export NO_PROXY=localhost,::1,.example.com
  5. Save your changes and exit the text editor

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 for all users.