Bash – How to list all shell variables

bashenvironment-variableszsh

Reading about this question: In zsh how can I list all the environment variables?, I wondered, how can I list all the shell variables?

Also, does the distinction between shell variables and environment variables apply to shells other than zsh?

I am primarily interested in Bash and Zsh, but it would be great to know how to do this in other mainstream shells.

Best Answer

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)