Skip to content

printenv Command Cheat Sheet

printenv prints all or part of the environment variables.


Synopsis

printenv [OPTION]... [VARIABLE]...

Basic Usage

printenv
Output:
SHELL=/bin/bash
USER=alice
PATH=/usr/local/bin:/usr/bin...
PWD=/home/alice

printenv HOME
# Output: /home/alice
Equivalent to echo $HOME.

printenv USER SHELL EDITOR

Check Exit Code

Useful in scripts to check if a variable exists.

printenv MISSING_VAR
# No output, exit code 1

Formatting

Null Terminator (-0)

End each output line with a NULL byte (\0) instead of a newline. Essential for parsing values that contain newlines.

printenv -0

printenv vs env

Feature printenv env
Purpose View variables run programs in modified environment
Set Vars? No Yes (env VAR=1 ./script)
Specific Vars? Yes (printenv HOME) No (must use grep)
Output Values only key=value pairs

Common Variables

  • PATH: List of directories to search for commands.
  • USER: Current user.
  • HOME: Home directory.
  • SHELL: Path to current shell.
  • EDITOR: Default text editor.
  • LANG: Locale settings.

Notes

  • printenv is a standalone binary.
  • set is a shell builtin that shows shell variables (including functions and non-exported vars). printenv only shows exported environment variables.