Introduction
Before we begin talking about how to set and list environment variables on Linux, let's briefly understand – What is an Environment Variable?
An environment variable stores values for software and OS. It provides essential information about the system's configuration, such as the location of important files or paths to directories.
Environment variables are commonly used to customize the behavior of software and enable seamless interaction between different programs. By defining these variables, developers can ensure that their applications run correctly across various systems. Understanding environment variables is crucial for software development and system administration.
In this tutorial, you will set and list environment variables on Linux. We will also address a few FAQs on how to set and list environment variables on Linux.
Advantages of Environment Variable
- Flexibility: Environment variables adapt software behavior across systems without code changes.
- Customization: Variables allow users to configure software to their specific needs.
- Security: Sensitive data, like API keys, can be securely stored and accessed through environment variables.
- Modularity: Variables enable modular code and easier integration of different software components.
- Portability: Programs become portable when using environment variables, working consistently across various environments.
Environment Variables and Shell Variables
The following is the format for variables:
KEY=value
KEY="Some other value"
KEY=value1:value2
- The variables' names are case-sensitive. Environment variables should be named in UPPER CASE by convention.
- When assigning several values to the variable, the colon
:
character must be used to separate them. - The equals
=
sign has no space surrounding it.
Environment variables and shell variables are the two basic groups of variables.
Environment variables are system-wide variables that are passed down to all spawned child processes and shells.
Variables that only pertain to the current shell instance are known as shell variables. Internal shell variables are unique to each shell, such as zsh
and bash
.
On Linux, there are numerous commands that may be used to list and set environment variables:
env
- This command allows you to run a different program in a different environment without having to change the current one. It prints a list of the current environment variables when used without an argument.printenv
- This command displays all or a subset of the environment variables.set
- This command changes the state of shell variables. It prints a list of all variables, including environment and shell variables, as well as shell functions, when used without an argument.unset
- This command removes the shell and environment variables from the command line.export
- This command is used to change the environment variables.
List Environment Variables
Printenv
is the most commonly used command for displaying environment variables. Only the value of the variable is displayed if the name of the variable is supplied as an argument to the command. Printenv
prints a list of all environment variables, one variable per line, if no argument is given.
To see the value of the HOME
environment variable, for example, type:
printenv HOME
The path of the currently logged in user will be printed in the output:
Output
/home/vegastack
You may also use the printenv
command with many arguments:
printenv LANG PWD
Output
en_US
/home/VegaStack
The printenv
or env
command will provide a list of all environment variables if called without any arguments:
printenv
You will get an output like below:
Output
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35;...
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=en_US
S_COLORS=auto
XDG_SESSION_ID=5
USER=linuxize
PWD=/home/linuxize
HOME=/home/linuxize
SSH_CLIENT=192.168.121.1 34422 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/linuxize
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LANGUAGE=en_US:
LOGNAME=linuxize
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv
Some of the most prevalent environmental variables are listed below:
USER
- The user who is currently logged in.HOME
- The current user's home directory.EDITOR
- To be used as the default file editor. When you typeedit
in your terminal, this is the editor that will be used.SHELL
- The current user's shell path, for example, bash or zsh.LOGNAME
- The current user's name is displayed here.PATH
- When running commands, this is a list of directories that will be searched. When you run a command, the system will look through those folders in this order and utilize the first executable that it finds.LANG
- The current settings for the locale.TERM
- The current terminal emulation.MAIL
- The current user's mail is saved at this location.
Only the environment variables are printed by the printenv
and env
commands. The set
command can be used to retrieve a list of all variables, including environment, shell and variables, and shell functions:
set
Output
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
You'll probably want to pipe the output to the less command because the command will display a long list of all variables.
set | less
The echo command can also be used to print a shell variable. To print the value of the BASH_VERSION
variable, for example, type:
echo $BASH_VERSION
Output
4.4.19(1)-release
Setting Environment Variables
We'll start with Shell Variables and then move on to Environment Variables to further show the differences between the two.
To create a new shell variable with the name MY_VAR
and the value VegaStack, simply type:
MY_VAR='VegaStack'
You can check if the variable is set by echo $MY_VAR
or filtering the set command output with grep set | grep MY VAR
:
echo $MY_VAR
Output
VegaStack
To see if this variable is an environment variable, use the printenv
command:
printenv MY_VAR
The variable's output will be empty, indicating that it is not an environment variable.
Attempting to print the variable in a new shell will result in empty output.
bash -c 'echo $MY_VAR'
Environment variables are set using the export
command.
Simply export the shell variable as an environment variable to generate an environment variable:
export MY_VAR
Run the following command to see if this is true:
printenv MY_VAR
Output
VegaStack
If you try to print the variable again in a fresh shell, the variable name will appear on your terminal:
bash -c 'echo $MY_VAR'
Output
VegaStack
You can also use a single line to set environment variables:
export MY_NEW_VAR="My New Var"
Environment Variables created in this way create variables that are only available in the current session. All variables will be lost if you open a new shell or log out.
Persistent Environment Variables
Environment variables must be defined in the bash configuration files if they are to be persistent. When you start a new session in most Linux versions, environment variables are read from the following files:
/etc/environment
- Set up system-wide environment variables with this file. Variables in this file are formatted as follows:
FOO=bar
VAR_TEST="Test Var"
/etc/profile
- When a bash login shell is entered, variables set in this file are loaded. You must use the export command to declare environment variables in this file:
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin
Shell configuration files are customized to each user. If you're using Bash, for example, you can declare variables in the ~/.bashrc
:
export PATH="$HOME/bin:$PATH"
Use the source command to load the new environment variables into the current shell session:
source ~/.bashrc
FAQs to Set and List Environment Variables on Linux
How can I permanently set an environment variable on Linux?
Add the export
statement to the appropriate configuration file (e.g., ~/.bashrc or /etc/profile) and re-login.
How can I see a list of all environment variables on Linux?
Use the printenv
command to display a comprehensive list of all environment variables.
What is the difference between local and global environment variables?
Local variables are only accessible within the current shell session, while global variables are accessible across multiple sessions.
Can environment variables contain spaces or special characters?
Yes, but they need to be properly enclosed in quotes. Example: export VARIABLE_NAME="value with spaces"
.
Can I export multiple variables in a single command?
Yes, separate the variable assignments with spaces. Example: export VAR1=value1 VAR2=value2
.
Can I assign multiple values to a single environment variable?
Yes, you can assign multiple values to a single environment variable by separating them with a colon (:) or any other delimiter of your choice.
What is the precedence order for environment variables?
The precedence order for environment variables is typically: local shell variables > environment variables set by the user > variables defined in system configuration files.
Conclusion
We've taught you how to set and list environment and shell variables in this tutorial.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.