How to list all variables names and their current values?
Including not only $HOME
, $PWD
etc but any other you have defined.
How 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:
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:
For more information about ZSH options, see
zshoptions
man page.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.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
, orprintenv
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...)To list the environment variables in terminal with CTRL+ALT+T you can use
env
command.for example :
etc.
hope that helps.
In bash using compgen:
If you want a specific environment variable, rather than printing them all with
printenv
, you can for example print it by doingecho "$PWD"
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):
Thanks to @tmgoblin for pointing out the use of compgen -v.
env is a POSIX 7 way:
Sample output:
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-printenvprintenv only lists exported variables, command+alt+$ ( "\e$": complete-variable ) will list all variables.
printenv
will show all the global environoment variables