Skip to Content
gnux

printenvx

Print all or part of the environment.

Mirrors GNU printenv. Run it as printenvx, or as gnux printenvx.

Synopsis

printenvx [OPTION]... [VARIABLE]...

GNU reference

Description

printenvx prints the value of each environment VARIABLE, one per line. With no VARIABLE it prints the whole environment as NAME=value lines. A VARIABLE that is not set prints nothing, not even an empty line, and makes the exit status 1, with no message; one that is set to the empty string prints an empty line. Names are matched exactly, including their case, on every platform. -0 ends each item with NUL instead of a newline, so values containing newlines survive.

gnux's terminal view (BR-13): on a terminal, a listing of the environment has each name colored apart from its value and the '=' dimmed, and the ':' between the directories of PATH and every other *PATH variable dimmed too; the text is GNU's. The value of a variable named as an operand is data and is never colored. --pretty[=WHEN] and --theme=NAME, options of gnux's own, choose the view (GNUX_PRETTY, GNUX_THEME); -0 and piped or redirected output are always GNU's.

Permissions

Needs no permissions. Reads only the process's own environment.

Use cases

  • Checking what value a variable has in the current environment.
  • Testing in a script whether a variable is set, from the exit status.
  • Dumping the environment into a CI log for debugging.
  • Comparing the environment before and after a change.
  • Reading a variable on Windows with the same command as on Linux.
  • Feeding the environment to another tool safely with -0.

Flags

      --help                     display this help and exit
  -0, --null                     end each output line with NUL, not newline
      --pretty WHEN[="always"]   the gnux terminal view WHEN: 'always' (the default when WHEN is omitted), 'auto' (the default: on a terminal only), or 'never'; GNUX_PRETTY sets the default
      --theme NAME               color the view with the built-in theme NAME (gnux theme picks and saves one); GNUX_THEME sets the default
      --version                  output version information and exit

Examples

printenvx
printenvx HOME
printenvx USER SHELL

Test whether a variable is set

printenvx CI > /dev/null && echox 'running in CI'

Show the search path one directory per line

printenvx PATH | trx ':' '\n'

Find variables by name

printenvx | sortx | headx -n 20
envx GREETING=hello printenvx GREETING
printenvx -0 | trx '\0' '\n' | wcx -l

Save the environment for a bug report

printenvx | sortx > env.txt

Read a variable in the portable gnux shell

gnux shell -c 'printenvx HOME'

Options

Each option has a page of its own, with its usage and examples.

  • -0, --null: End each output line with NUL, not newline.
  • --pretty: The gnux terminal view WHEN: 'always' (the default when WHEN is omitted), 'auto' (the default: on a terminal only), or 'never'; GNUX_PRETTY sets the default.
  • --theme: Color the view with the built-in theme NAME (gnux theme picks and saves one); GNUX_THEME sets the default.

Exit status

StatusMeaning
0every VARIABLE was set (or none was named)
1at least one VARIABLE was not set
2a command-line mistake

Help in the terminal

printenvx --help                # options, first examples, exit status
gnux help printenvx             # this page: the full manual
gnux help printenvx --OPTION    # one option in depth, with its examples

See also

On this page