How to list all variables names and their current values?
Including not only $HOME
, $PWD
etc but any other you have defined.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityHow to list all variables names and their current values?
Including not only $HOME
, $PWD
etc but any other you have defined.
For bash: (the standard shell in Ubuntu)
Enter the following command in a terminal to print all the environment variables:
printenv
For further information about this command, read the printenv
man page.
To show a list including the "shell variables" you can enter the next command:
( set -o posix ; set ) | less
This will show you not only the shell variables, but the environment variables too.
For more information related with this topic read:
For zsh: (an advanced shell)
Use the following command:
( setopt posixbuiltin; set; ) | less
For more information about ZSH options, see zshoptions
man page.
MYNEWVARIABLE=Ubuntu
and execute printenv
it doesn't show there. Why is that, and how do those others show up?
Mar 30, 2013 at 3:30
export MYNEWVARIABLE=Ubuntu
and it will work as you expect.
set
, it lists the variable created by you as well. Or do set | grep myvar
Jan 4, 2015 at 15:01
printenv
is an external command, so it only knows about (and prints) exported environment variables. set
is an internal bash command, so it shows all the "shell variables" (unexported environment variables) as well as the exported environment variables.
Jul 30, 2015 at 15:33
unset MYNEWVARIABLE; MYNEWVARIABLE=Ubuntu; printenv | grep MYNEW
, but this will unset MYNEWVARIABLE; export MYNEWVARIABLE=Ubuntu; printenv | grep MYNEW
, and this will unset MYNEWVARIABLE; MYNEWVARIABLE=Ubuntu printenv | grep MYNEW
. Using export says "the variable I'm setting should be part of the environment that gets passed to processes, not just a variable in this shell." My third example says "the variable should be part of the environment that gets passed to THIS process, but not stick around afterward."
Aug 26, 2016 at 17:39
You can see all variables with the declare
builtin.
declare -p
If you're only interested in environment variables, use
declare -xp
Run help declare
to see what the other options are.
POSIXLY_CORRECT=1 set
and it is also worthy of mention that declare
is alias
(in that context) for typeset
, another bash builtin.
compgen -e
.
I know that this question is quite old and answered, but I think I can add a bit of useful information.
In all the methods described above, the procedure that is suggested is:
env
, or
printenv
or whateverThe problem of these solutions are that you are seeing the environment variables of the shell that is running into the terminal. You are not seeing the environment variables available to an application run, for example, directly by the graphic interface.
This is noticeable if, for example, you use your ~/.profile
, or .bashrc
, or .zshenv
(depending on your shell) to modify the environment variables --- like the classic addition of directories to the path.
To see the environment variables available to the application started directly in the graphic environment, you can do the following (in Gnome Shell, I am sure there is an equivalent method in all the other DE):
xterm -e bash --noprofile --norc
(Or, if you do not have xterm
, gnome-terminal -- bash --noprofile --norc
--- thanks to @Mike Nakis for the comment).
You now have a terminal with a shell that did not add any environment variables. You can use env
here to list all your environment variables:
Obviously the new shell will have the environment variables added by the system files, but that variables should be available (by inheritance) to all programs in the system anyway.
I am posting this because it's the fourth time I have to search this trick again, checking my .pam_environment
file. So now I will find it faster (and in the process, I hope helping someone else...)
xterm -e bash --noprofile --norc
because the startup files are not read and executed when Ubuntu is started with graphical interface ? See askubuntu.com/q/1025845/1471
gnome-terminal -- bash --noprofile --norc
Apr 28, 2019 at 11:10
To list the environment variables in terminal with CTRL+ALT+T you can use env
command.
for example :
[raja@localhost ~]$ env
XDG_VTNR=1
SSH_AGENT_PID=3671
XDG_SESSION_ID=3
HOSTNAME=localhost.localdomain
IMSETTINGS_INTEGRATE_DESKTOP=yes
GPG_AGENT_INFO=/home/raja/.gnupg/S.gpg-agent:3691:1
GLADE_PIXMAP_PATH=:
TERM=xterm-256color
SHELL=/bin/bash
XDG_MENU_PREFIX=xfce-
DESKTOP_STARTUP_ID=
HISTSIZE=1000
XDG_SESSION_COOKIE=0250277dd805498381e96c05d88068b0-1364679772.845276-1676152201
WINDOWID=65011716
GNOME_KEYRING_CONTROL=/home/raja/.cache/keyring-N3QoQ2
IMSETTINGS_MODULE=none
QT_GRAPHICSSYSTEM_CHECKED=1
USER=raja
etc.
hope that helps.
In bash using compgen:
compgen -v | while read line; do echo $line=${!line};done
declare -p
won't include things like BASH_COMMAND
, BASH_SUBSHELL
, COMP_WORDBREAKS
, HISTCMD
, LINENO
, RANDOM
, SECONDS
.
declare -p
"includes" resp. lists them all (and FUNCNAME
additionally), but you are kind of right: the values are not shown and also values of arrays are not shown. do you know why this is? -- so IMHO your line is the most comprehensive way to do this, even with all values of arrays including indexes!
Jun 22 at 10:53
Most solutions here either print only environment variables, or have the drawback that env or (set -o posix; set) do not print values in easily parseable form (try to print variable A=$'a\r\nb', it has multiple lines...).
Here is a function that will print all variables, one variable per line, in the POSIX escaped form (works properly for simple text variables, does not work for arrays):
function dump_vars {
local VARNAME
compgen -v | while read -r VARNAME; do
printf "$VARNAME=%q\n" "${!VARNAME}"
done
}
Thanks to @tmgoblin for pointing out the use of compgen -v.
If you want a specific environment variable, rather than printing them all with printenv
, you can for example print it by doing echo "$PWD"
echo $PATH
or echo $USER
. This will print the environment variable's value. The dollar sign operator is the only thing needed to access the value of a variable or command.
Jun 8 at 19:42
env is a POSIX 7 way:
export asdf=qwer
env | grep asdf
Sample output:
asdf=qwer
It only shows exported variables: non-exported variables are not usually considered "environment variables".
Prefer that over printenv
, which is not POSIX. Both seem to do the same thing without arguments: https://unix.stackexchange.com/questions/123473/what-is-the-difference-between-env-and-printenv
env
more visible with better formatting, and I've added more info.
Nov 30, 2015 at 15:01
printenv
will show all the global environoment variablesprintenv only lists exported variables, command+alt+$ ( "\e$": complete-variable ) will list all variables.
Script to dump everything, sort, and highlight all $ENVS and $variables declared in the session.
#!/bin/bash
temp="$(mktemp)"
declare > $temp
printenv >> $temp;
sort -u $temp -o $temp;
grep --color=auto -E '^[A-Za-z_]+[^=]+' $temp
rm -i $temp