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.

(set -o posix ; set)

@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."

You will see that set provides output while env does not.

  • > (set -o posix ; set) I did not know that non-exported variables go to a subshell. Aaah...: stackoverflow.com/questions/51903718/… Commented Mar 21, 2021 at 18:25
  • I get set: no such option: posix. Commented Oct 31, 2022 at 14:29
  • @hugoderhungrige what version of bash are you using? bash --version
    – Starfish
    Commented Nov 30, 2022 at 22:02
  • @Starfish 5.1.16(1)-release Commented Dec 1, 2022 at 10:36

The env command with no arguments will print a list of the "exported" environment variables and their values. These variables are made visible to subprocesses - many other environment variables are not shown with this, and used inside the running shell only, eg for configuration.

  • 13
    I prefer printenv, since both env and set have other functions than just outputting the environment.
    – iglvzx
    Commented May 4, 2012 at 5:04
  • 6
    env will only print a list of environment variables that have been marked for export. It will not print all variables.
    – Starfish
    Commented Sep 7, 2013 at 5:19
  • 5
    @Starfish is right - that means this accepted answer was very wrong. Edited to say it's listing exported variables only. Commented Jul 1, 2014 at 0:12
  • Why isn't this the top answer. It's so much simpler and more memorable.
    – stevec
    Commented Jan 25, 2020 at 8:39
compgen -v

prints shell variables (but not the values).

compgen -e

prints exported variables i.e. those that get inherited by processes this shell starts (but not their values).

Difference between shell and exported variables: https://unix.stackexchange.com/questions/3507/difference-between-environment-variables-and-exported-environment-variables-in-b?rq=1


I arrived here from a search for how to order the output of env alphabetically; it's quite simple

env | sort -f
  • the -f option makes sort ignore case, which is what you probably want 99% of the time

Combining some of the previous answers, I find the following useful:

compgen -v  | sort | while read var; do [ -z "${!var}" ] || echo $var=${!var} ; done
  • compgen -v : Print all variables names, either local or exported.
  • sort : Alphabetical order.
  • ${!var} : Get the value of $var (variable indirection).
  • -z condition : Check if value is null, else print "variable=value".

Note: printenv is great also, but it will only show a list of environment variables that have been exported, not local/bash variables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .