printenv Command Cheat Sheet
printenv prints all or part of the environment variables.
Synopsis
printenv [OPTION]... [VARIABLE]...
Basic Usage
Print All Variables
printenv
SHELL=/bin/bash
USER=alice
PATH=/usr/local/bin:/usr/bin...
PWD=/home/alice
Print Specific Variable
printenv HOME
# Output: /home/alice
echo $HOME.
Print Multiple Variables
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
printenvis a standalone binary.setis a shell builtin that shows shell variables (including functions and non-exported vars).printenvonly shows exported environment variables.