How to list all variables names and their current values?
How to list all variables names and their current values? Including not only $HOME , $PWD etc but any other you have defined.
You’ve accepted an incorrect answer. «printenv» only gives you the environment variables. If you truly want all variables currently declared in your shell, use «declare -p» or «typeset -p».
11 Answers 11
For bash: (the standard shell in Ubuntu)
Enter the following command in a terminal to print all the environment variables:
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:
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.
If I go to the terminal and write MYNEWVARIABLE=Ubuntu and execute printenv it doesn’t show there. Why is that, and how do those others show up?
Probably you are seeing the difference between a shell variable and an environment variable. Try export MYNEWVARIABLE=Ubuntu and it will work as you expect.
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.
To expand on @Rmano’s reply to @Strapakowsky. This will not work 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.»
You can see all variables with the declare builtin.
If you’re only interested in environment variables, use
Run help declare to see what the other options are.
this is far neat-er solution than POSIXLY_CORRECT=1 set and it is also worthy of mention that declare is alias (in that context) for typeset , another bash builtin.
@maoizm If you want only the variable names and nothing else, it’s easier to use compgen for that. 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:
The 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):
(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. )
How do I see a list of all currently defined environment variables in a Linux bash terminal?
In a Linux bash terminal, there are often many environment variables that have been set, like $PATH and $HOME . Is it possible to see all of the environment variables that have been set? How?
5 Answers 5
TL;DR: use (set -o posix ; set)
According to the Bash manual you can use the set built-in command to show all of the environment variables that have been set. The set command will also display the definitions of any functions. If you only want to see the variables, and not the functions, then you can turn on POSIX mode before running the set command. The easiest way to do that is with set -o posix , but that will leave POSIX mode on until you turn it off with set +o posix .
Therefore, the following command will show all of the defined environment variables by using a subshell without affecting POSIX compliance in your current shell.
@RedGrittyBrick and @iglvzx suggested using the env command, however this command will not provide a complete listing of environment variables. env will only show the varaibles that have been marked for export. Compare the output of env | sort and export -p and you will see what I mean. You can run comm -23 <(set -o posix; set) <(env|sort) if you want to see which environment variables are not being exported.
The reason for the discrepancy is that env is a separate executable as opposed to set which is a shell built-in command. According to the Bash manual, when a command is executed that is not a shell built-in command or function it will only receive environment variables that have been marked for export in Bash. There are many variables that are not exported. Therefore, if you wish to see all of the variables that your shell has defined, you must use the set command as stated in the manual.
You can easily test this behavior for yourself using the following commands.
MY_TEST_VARIABLE="This is my test variable." set | grep MY_TEST_VARIABLE env | grep MY_TEST_VARIABLE
You will see that set provides output while env does not.
What are my environment variables? [closed]
Wow that was fast! I guess all the command do the trick. The export command gave me a lot of «declare -x» in front. Thanks guys!
It was inappropriate to close this question as off topic. When programming on Linux, as I am doing at the moment, it is often useful to discover what the environmental variables are. Quite a lot of people have found this to be a useful question, including me.
4 Answers 4
I am not sure if thats what you want, but try printenv
This will show you all your environment variables.
Just execute env in a terminal.
$ env TERM=xterm SHELL=/bin/bash USER=joksnet USERNAME=joksnet DESKTOP_SESSION=gnome PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games PWD=/home/joksnet GDM_KEYBOARD_LAYOUT=us LANG=en_US.utf8 HOME=/home/joksnet DISPLAY=:0.0 COLORTERM=gnome-terminal _=/usr/bin/env
Type export without any parameters.
SET(P) POSIX Programmer’s Manual SET(P) NAME set - set or unset options and positional parameters SYNOPSIS set [-abCefmnuvx][-h][-o option][argument. ] set [+abCefmnuvx][+h][+o option][argument. ] set -- [argument. ] set -o set +o DESCRIPTION If no options or arguments are specified, set shall write the names and values of all shell variables in the collation sequence of the current locale. Each name shall start on a separate line, using the format: "%s=%s\n", , The value string shall be written with appropriate quoting; see the description of shell quoting in Quoting . The output shall be suitable for reinput to the shell, setting or resetting, as far as possible, the variables that are currently set; read-only variables cannot be reset.
env or printenv are better. In bash, set will also print all your defined functions, which on a system like ubuntu, is a very long printout.