6

Environment variables can be shown with env; but, some are not shown. For example...

echo $EUID might produce as result of 1000 yet env | grep EUID produces no result.

What is this type of variable? A read-only environment variable?

Do all shells set the same variables by some convention?

How does one go about listing these hidden variables?

Christopher
  • 15,611
  • 7
  • 51
  • 64
  • 2
    see also: http://unix.stackexchange.com/questions/3510/how-to-print-all-environment-variables-defined-but-not-necessarily-exported-in – Lesmana Mar 17 '13 at 17:52
  • 1
    Not all shell variables are environment variables, only those that are marked for export by, e.g., the `export` command. – chepner Mar 24 '13 at 03:18
  • Related (on [ubuntu.se]): [Why aren't variables like $PS1 in printenv?](https://askubuntu.com/questions/960551/why-arent-variables-like-ps1-in-printenv) – Eliah Kagan Oct 23 '17 at 17:48

2 Answers2

10

The set command shows all variables (and functions), not just the exported ones, so

set | grep EUID

will show you the desired value. This command should show all the non-exported variables:

comm -23 <(set | grep '^[^=[:space:]]\+=' | sort) <(env | sort)
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
  • Unfortunately 'set' can add quotes while 'env' does not as such this will list some variables (often very long ones), which are actually exported environment variables. – anthony Jun 08 '16 at 00:41
1

There are no hidden environment variables.
All are printed with either env or printenv.

What you did was print the value of a variable EUID, but that variable is not exported.

$ bash -c 'declare -p EUID'
declare -ir EUID="1000"

That is: (i) for integer and (r) for readonly. No (x) for exported, though.

$ zsh -c 'typeset -p EUID'
typeset -i10 EUID=1000

That is (i) for integer, (10) for base 10 (decimal).

Instead:

$ bash -c 'declare -p PATH'
declare -x PATH="…"

$ zsh -c 'typeset -p PATH'
export PATH=…