7

(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.

5
  • Could you explain why? This sounds like an XY problem, what problem do you need to solve that requires knowing whether the variable is exported?
    – terdon
    Commented Apr 8, 2016 at 8:13
  • @terdon: because it is the easiest way to debug a problem that I have with a binary that is not behaving properly. I have the impression that the environment is not set up properly: although the variable is set, I think the binary is not having access to it in its environment. It could be another problem, but I would first like to make sure that the variable is properly exported in the current shell.
    – blueFast
    Commented Apr 8, 2016 at 8:20
  • 2
    If you know the name of the variable, and can see that it isn't set, then it isn't exported. The thing is that what you're asking here gets quite complex when you take into account local shell variables, global environment variables etc. Simply put, if the variable is set in the parent shell but not available to the program, it is not exported.
    – terdon
    Commented Apr 8, 2016 at 8:22
  • @terdon: and also because I am curious.
    – blueFast
    Commented Apr 8, 2016 at 8:22
  • 2
    Whatever is listed in env / printenv is "exported", shell variables are just not listed. What do you mean by "exported"? If it has been explicitly exported using export? Or just if it's in the environment?
    – kos
    Commented Apr 8, 2016 at 8:25

1 Answer 1

16

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()]'

You must log in to answer this question.

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