Home / Alt manpages / printenv(1)

  • printenv(1)
  • User command
  • linux

Inspect Environment Variables Safely with printenv

A missing environment variable is one of the quieter ways a script breaks, and printenv is the fastest way to check before that happens. This covers reading one variable, several, or the whole environment, catching a missing name from the exit status, and producing output that is safe for another program to parse. The examples use GNU printenv 9.4 from Ubuntu's coreutils package, coreutils 9.4-3ubuntu6.3.

Allow about ten minutes. You need a shell and an ordinary user account; this only reads process environment data and needs no sudo.

Warning

Environment variables often hold tokens, service credentials or private paths. Treat the output as potentially sensitive, and do not paste it into a ticket or shell history without checking it first.

1. Confirm which printenv will run

A shell can provide its own printenv with different options, so resolve the actual command before trusting its behaviour:

$ command -V printenv
printenv is /usr/bin/printenv
$ /usr/bin/printenv --version
printenv (GNU coreutils) 9.4

The absolute path keeps the rest of this guide unambiguous. If command -V reports a shell builtin or an alias, either check that shell's documentation, or use /usr/bin/printenv directly when you need the GNU behaviour shown here.

Checkpoint

You have identified the executable and its version. No path found means stop here and install the package through your normal distribution process, rather than copying an unrelated implementation into a system directory.

2. Print one selected variable

Pass the variable name as an argument, and the command prints only its value followed by a newline:

$ /usr/bin/printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Your own PATH will differ. Do not assume a variable exists just because it is common, and remember a value can contain spaces, punctuation or be empty: printenv adds no shell quoting of its own.

To test with known, harmless values without touching your real environment, build a temporary one for the command:

$ env -i DEMO=one OTHER='two words' /usr/bin/printenv DEMO OTHER
one
two words

env -i clears the environment for that one process only. It does not touch your current shell, so there is nothing to undo afterwards.

3. Read every variable, or select several

With no names given, printenv emits name-value pairs for the whole environment:

$ /usr/bin/printenv
SHELL=/bin/bash
USER=operator
PATH=/usr/local/bin:/usr/bin:/bin

That complete output is host-specific and can include secrets, so prefer a short allow-list of names for routine diagnostics instead. Pass several names in one call and each value lands on its own line:

$ /usr/bin/printenv HOME SHELL
/home/operator
/bin/bash

If a value itself contains a newline, line-oriented output becomes ambiguous. Use the NUL form below when another program needs to parse arbitrary values.

4. Use NUL delimiters for scripts

-0 or --null ends each selected value with a NUL byte instead of a newline, useful when a value may itself contain newlines and a normal delimiter would be ambiguous:

$ env -i DEMO=one OTHER=two /usr/bin/printenv --null DEMO OTHER | od -An -t x1
 6f 6e 65 00 74 77 6f 00

Those hexadecimal 00 bytes are the delimiters. A consumer has to be built for NUL-delimited input; do not pipe this to a normal line-oriented tool and assume it parsed two records correctly.

For a shell check that only needs one value, command substitution strips trailing newlines, which can hide data-shape problems. Prefer a direct command or a NUL-aware consumer when exact bytes matter.

5. Handle an unset name as a real error

A missing variable produces no value for that name and exits with status 1. Check the status right away:

$ env -i DEMO=one /usr/bin/printenv MISSING
$ printf 'status=%s\n' "$?"
status=1

With several names, existing values still print, but a single missing name still fails the whole command:

$ env -i DEMO=one /usr/bin/printenv DEMO MISSING
one
$ printf 'status=%s\n' "$?"
status=1

That detail matters in scripts: do not treat any output as proof every requested variable was found. Capture the status, or use a conditional:

$ if value=$(/usr/bin/printenv REQUIRED_SETTING); then
>     printf 'setting is present\n'
> else
>     printf 'setting is missing\n' >&2
>     exit 1
> fi
setting is missing

This checks presence, not whether the value is non-empty. An explicitly exported variable with an empty value differs from an unset one, but both print an empty line, so test the shell's variable state too if that distinction matters.

6. Keep the boundary clear

printenv reads only the environment inherited by the process. It cannot see shell variables that have not been exported, and it never changes anything in the parent shell:

$ LOCAL_ONLY='not exported'
$ /usr/bin/printenv LOCAL_ONLY
$ printf 'status=%s\n' "$?"
status=1
$ export LOCAL_ONLY
$ /usr/bin/printenv LOCAL_ONLY
not exported

export changes the current shell's environment for later child processes; run unset LOCAL_ONLY to undo that example. Do not export credentials merely to make them easier to inspect; pass sensitive data through a safer mechanism when the consuming program supports one.

Done means

  • Command resolved. You confirmed whether the shell selected GNU /usr/bin/printenv.
  • Selection works. You can print one name, several names, or the complete environment.
  • Status checked. Status 1 is treated as a real signal that a requested variable is absent.
  • NUL used where needed. --null gets reached for when line delimiters are unsafe for a parser.
  • Export boundary understood. Unexported shell variables are known to be invisible to child processes.
  • No system changes. Nothing here required elevated privileges or configuration changes.