← All commands
THE COMMAND REFERENCE

printenv

Print the value of environment variables.

Last updated

Portable / common

Implementation-level reference; examples are not certified for every installed version.

Overview

printenv prints the value of the environment variable you name, or all variables when you name none. It exits with status 1 when a named variable is not set, which makes it useful in if tests. It shows only exported variables, those that programs started by the shell can see.

Background

The printenv command appeared in 3.0BSD, according to the macOS manual, which also says it is not specified by any standard. Today the name covers GNU printenv, the printenv that macOS ships, and the printenv applet in BusyBox. They agree on printing one variable's value and on the exit status for a missing one.

Examples

Print the value of one variable:

FOO=bar printenv FOO

Check whether a variable is set; the exit status is 1 when it is not:

printenv NOPE || echo unset

Print every variable and count the line that holds PATH:

printenv | grep -c '^PATH='

Print the value in a script without the name:

FOO=bar sh -c 'echo "value is $(printenv FOO)"'

Common mistakes

A shell variable that was never exported is not in the environment, so printenv NAME fails for it even though echo "$NAME" works; use export. printenv NAME prints only the value, while printenv alone prints NAME=value lines. printenv is not in POSIX; env and echo "$NAME" are the portable choices.

Portability

Only printing one variable and listing all are in the Common examples. GNU has -0 for NUL-terminated output. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.