- How to Set and List Environment Variables in Linux
- Environment Variables and Shell Variables #
- List Environment Variables #
- Setting Environment Variables #
- Persistent Environment Variables #
- Conclusion #
- List Environment Variables in Linux
- 1. Overview
- 2. Using the printenv Command
- 3. Using the env Command
- 4. Using the set Command
- 5. Using the declare Command
- 6. Using the echo Command
- 7. Conclusion
- How to list all Linux environment variables including LD_LIBRARY_PATH
- 3 Answers 3
- Related
- Hot Network Questions
- Subscribe to RSS
How to Set and List Environment Variables in Linux
In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, an environment variable is a variable with a name and an associated value.
Environment variables allow you to customize how the system works and the behavior of the applications on the system. For example, the environment variable can store information about the default text editor or browser, the path to executable files, or the system locale and keyboard layout settings.
In this guide, we will explain to read and set environment and shell variables.
Environment Variables and Shell Variables #
Variables have the following format:
KEY=value KEY="Some other value" KEY=value1:value2
- The names of the variables are case-sensitive. By convention, environment variables should have UPPER CASE names.
- When assigning multiple values to the variable they must be separated by the colon : character.
- There is no space around the equals = symbol.
Variables can be classified into two main categories, environment variables, and shell variables.
Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells.
Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash , has its own set of internal shell variables.
There are several commands available that allow you to list and set environment variables in Linux:
- env – The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.
- printenv – The command prints all or the specified environment variables.
- set – The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.
- unset – The command deletes shell and environment variables.
- export – The command sets environment variables.
List Environment Variables #
The most used command to displays the environment variables is printenv . If the name of the variable is passed as an argument to the command, only the value of that variable is displayed. If no argument is specified, printenv prints a list of all environment variables, one variable per line.
For example, to display the value of the HOME environment variable you would run:
The output will print the path of the currently logged in user:
You can also pass more than one arguments to the printenv command:
If you run the printenv or env command without any arguments it will show a list of all environment variables:
The output will look something like this:
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
Below are some of the most common environment variables:
- USER — The current logged in user.
- HOME — The home directory of the current user.
- EDITOR — The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
- SHELL — The path of the current user’s shell, such as bash or zsh.
- LOGNAME — The name of the current user.
- PATH — A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
- LANG — The current locales settings.
- TERM — The current terminal emulation.
- MAIL — Location of where the current user’s mail is stored.
The printenv and env commands print only the environment variables. If you want to get a list of all variables, including environment, shell and variables, and shell functions you can use the set command:
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=()
The command will display a large list of all variables so you probably want to pipe the output to the less command.
You can also use the echo command to print a shell variable. For example, to print the value of the BASH_VERSION variable you would run:
Setting Environment Variables #
To better illustrate the difference between the Shell and Environment variables we’ll start with setting Shell Variables and then move on to the Environment variables.
To create a new shell variable with the name MY_VAR and value Linuxize simply type:
You can verify that the variable is set by using either echo $MY_VAR of filtering the output of the set command with grep set | grep MY_VAR :
Use the printenv command to check whether this variable is an environment variable or not:
The output will be empty which tell us that the variable is not an environment variable.
You can also try to print the variable in a new shell and you will get an empty output.
The export command is used to set Environment variables.
To create an environment variable simply export the shell variable as an environment variable:
You can check this by running:
If you try to print the variable in a new shell this time you will get the variable name printed on your terminal:
You can also set environment variables in a single line:
export MY_NEW_VAR="My New Var"
Environment Variables created in this way are available only in the current session. If you open a new shell or if you log out all variables will be lost.
Persistent Environment Variables #
To make Environment variables persistent you need to define those variables in the bash configuration files. In most Linux distributions when you start a new session, environment variables are read from the following files:
- /etc/environment — Use this file to set up system-wide environment variables. Variables in this file are set in the following format:
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin
To load the new environment variables into the current shell session use the source command:
Conclusion #
In this guide, we have shown you how to set and list environment and shell variables.
Feel free to leave a comment if you have any questions.
List 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
An environment variable is a dynamic object that defines a location to store some value. We can change the behavior of the system and software using an environment variable. Environment variables are very important in computer programming. They help developers to write flexible programs.
There are multiple ways to list or display an environment variable in Linux. We can use the env, printenv, declare, or set command to list all variables in the system.
In this tutorial, we’ll explain how to list environment variables in Linux.
2. Using the printenv Command
The printenv command-line utility displays the values of environment variables in the current shell.
We can specify one or more variable names on the command line to print only those specific variables. Or, if we run the command without arguments, it will display all environment variables of the current shell.
For example, we can use the printenv command followed by HOME to display the value of the HOME environment variable:
In addition, we can specify multiple environment variables with the printenv command to display the values of all the specified environment variables:
Let’s display the values of the HOME and SHELL environment variables:
$ printenv HOME SHELL /root /bin/bash
We can see a list of all of the environment variables using the printenv command without any arguments:
$ printenv XDG_VTNR=7 XDG_SESSION_ID=c1 SHELL=/bin/bash TERM=xterm USER=root SUDO_USER=vyom SUDO_UID=1000 USERNAME=root MAIL=/var/mail/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games .
3. Using the env Command
env is another shell command we can use to print a list of environment variables and their values. Similarly, we can use the env command to launch the correct interpreter in shell scripts.
We can run the env command without any arguments to display a list of all environment variables:
$ env XDG_VTNR=7 XDG_SESSION_ID=c1 SHELL=/bin/bash TERM=xterm USER=root SUDO_USER=vyom SUDO_UID=1000 USERNAME=root MAIL=/var/mail/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games .
We can use the grep command to filter the value of the specific variable we’re interested in:
$ env | grep USERNAME USERNAME=root
4. Using the set Command
set is yet another command-line utility for listing the names and values of each shell variable. Although the set command has other uses, we can display the names and values of all shell variables in the current shell simply by running it without any options or arguments:
$ set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="3" [2]="11" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") .
5. Using the declare Command
declare is another built-in command used to declare a shell variable and display its values.
For example, let’s run the declare command without any option to print a list of all shell variables in the system:
$ declare BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="3" [2]="11" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") .
6. Using the echo Command
echo is also used to display values of the shell variable in Linux.
For example, let’s run the echo command to display the value of the $HOSTNAME variable:
7. Conclusion
In this short article, we explained how to list environment variables using different Linux commands.
How to list all Linux environment variables including LD_LIBRARY_PATH
How to list all the environment variables in Linux? When I type the command env or printenv it gives me lots of variables, but some variables like LD_LIBRARY_PATH and PKG_CONFIG don’t show up in this list. I want to type a command that list all the environment variables including this variables ( LD_LIBRARY_PATH and PKG_CONFIG )
3 Answers 3
env does list all environment variables.
If LD_LIBRARY_PATH is not there, then that variable was not declared; or was declared but not export ed, so that child processes do not inherit it.
If you are setting LD_LIBRARY_PATH in your shell start-up files, like .bash_profile or .bashrc make sure it is exported:
export LD_LIBRARY_PATH=/usr/local/lib:$
This will modify the variable.
To print it, type: echo $LD_LIBRARY_PATH and it should show the above value.
If you’re seeing nothing when you print that, then the variable might not be set.
The question in fact is a good question. when run env or printenv , the output will be the system environment, but LD_LIBRARY_PATH is not belong to.
For example, if you set a=1 , you can’t show it by env . Same as LD_LIBRARY_PATH, it is used by ld.so only(ld. so – this little program that starts all your applications)
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.