Ubuntu – List all environment variables, and show if they are exported or not

bash

(This question is similar to this one and this one, but with the added requirement that I would like to know if variables are exported or not)

I am in bash, Ubuntu 14.04.4 LTS. I would like to:

  • list all environment variables
  • for each variable, I would like to see if it is exported or not

Additionally, I would like to list only the exported environment variables.

Best Answer

Use the declare builtin:

$ help declare
declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
    Set variable values and attributes.

    Declare variables and give them attributes.  If no NAMEs are given,
    display the attributes and values of all variables.

    Options:
      -f    restrict action or display to function names and definitions
      -F    restrict display to function names only (plus line number and
        source file when debugging)
      -g    create global variables when used in a shell function; otherwise
        ignored
      -p    display the attributes and value of each NAME

    Options which set attributes:
      -a    to make NAMEs indexed arrays (if supported)
      -A    to make NAMEs associative arrays (if supported)
      -i    to make NAMEs have the `integer' attribute
      -l    to convert NAMEs to lower case on assignment
      -n    make NAME a reference to the variable named by its value
      -r    to make NAMEs readonly
      -t    to make NAMEs have the `trace' attribute
      -u    to convert NAMEs to upper case on assignment
      -x    to make NAMEs export

Accordingly:

$ declare -p
declare -- BASH="/usr/bin/bash"
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:nullglob:progcomp:promptvars:sourcepath"
declare -ir BASHPID
declare -A BASH_ALIASES='()'
declare -a BASH_ARGC='()'
declare -a BASH_ARGV='()'
declare -A BASH_CMDS='()'
declare -- BASH_COMMAND
declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d"
declare -a BASH_LINENO='()'
declare -a BASH_SOURCE='()'
declare -- BASH_SUBSHELL
declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="42" [3]="1" [4]="release" [5]="x86_64-unknown-linux-gnu")'
declare -- BASH_VERSION="4.3.42(1)-release"
declare -x CDPATH=":/home/muru"
declare -x COLORTERM="gnome-terminal"
declare -- COLUMNS="237"
declare -- COMP_WORDBREAKS
declare -x CONFLOCAL="laptop"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x DESKTOP_AUTOSTART_ID="10abca3e1337cb662146002998494759100000008000003"
declare -x DESKTOP_SESSION="gnome"
declare -a DIRSTACK='()'
declare -x DISPLAY=":0"
declare -x EDITOR="vim"
…

To see just exported environment variables, use declare -px:

$ declare -px
declare -x CDPATH=":/home/muru"
declare -x COLORTERM="gnome-terminal"
declare -x CONFLOCAL="laptop"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x DESKTOP_AUTOSTART_ID="10abca3e1337cb662146002998494759100000008000003"
declare -x DESKTOP_SESSION="gnome"
declare -x DISPLAY=":0"
declare -x EDITOR="vim"
declare -x GDMSESSION="gnome"
…

Or use external commands, like env, printenv, or:

awk 'BEGIN{for (i in ENVIRON) {print i}}'
perl -e 'print "$_\n" for keys(%ENV)'
python -c 'import os; [print(k) for k in os.environ.keys()]'
Related Question