How can I list all shell variables?

List all shell variables

bash : use set -o posix ; set. The POSIX options is there to avoid outputting too much information, like function definitions. declare -p also works.

zsh : use typeset

Shell variables and environment variables

An environment variable is available to exec()-ed child processes (as a copy. if parent process change the variable, the child environment is not updated). A non-environment variable is only available to the current running shell and fork()-ed subshells. This distinction is present in all shells.

(completed thanks to comments)


There are many alternatives:

printenv

Print the values of the specified environment VARIABLE(s). If no VARIABLE is specified, print name and value pairs for them all.

env

env - run a program in a modified environment

export

Set an environment variable. Mark each name to be passed to child processes in the environment.....

-p Display output in a form that may be reused as input.

If no names are supplied, or if the `-p' option is given, a list of exported names is displayed.

set

is useful to get shell variables as well.

If you need extra info (integer, exported) you should instead use

typeset

export has an advantage, that its output can be immediately read back onto the shell.

Lastly, there is

compgen -v

Display possible completions depending on the options.

which shows all variables, shell and environment, without their value or extra info. You will have to echo $VARIABLE_NAME to find the variable value. But at least the list is complete. It belongs to bash, not zsh.


With zsh, you can use typeset, which gives more information than set, e.g. the type of the variables. You can still filter the output with grep or sed, depending on what you want. Environment variables are marked as exported in the output.