LinuxCommandLibrary

printenv

Display environment variables

TLDR

Display key-value pairs of all environment variables

$ printenv
copy

Display the value of a specific variable
$ printenv [HOME]
copy

Display the value of a variable and end with NUL instead of newline
$ printenv [[-0|--null]] [HOME]
copy

SYNOPSIS

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

PARAMETERS

VARIABLE...
    One or more names of environment variables to display. If no VARIABLE is specified, all environment variables are printed.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

printenv is a simple yet powerful command-line utility used to display the values of environment variables. When invoked without any arguments, it prints a list of all environment variables and their corresponding values, one per line. This is useful for inspecting the current execution environment of a shell or script.

If one or more variable names are provided as arguments, printenv will only display the values of those specific variables. If a requested variable is not found in the environment, nothing is printed for that variable, and the command exits with a non-zero status. This behavior makes it suitable for use in shell scripts to check for the existence and value of particular environment variables.

While similar to the env command (when used without arguments) and the shell built-in set command, printenv specifically focuses on environment variables, which are inherited by child processes. It does not display shell-specific variables or functions that are not exported to the environment.

CAVEATS

printenv only displays environment variables that have been exported. It does not show shell-specific variables or functions that are not part of the environment (e.g., local shell variables set with VAR=value without export). For a comprehensive list of shell variables, aliases, and functions, consult your shell's built-in commands like set or declare.

EXIT STATUS

printenv exits with status 0 (success) if all specified VARIABLEs were found and printed, or if no VARIABLEs were specified. It exits with a non-zero status (failure) if at least one specified VARIABLE was not found in the environment.

DISTINCTION FROM ENV AND SET

While often used interchangeably, printenv, env, and set have subtle differences:

printenv: Displays only the environment variables. Cannot modify the environment or execute a command.

env: Can display environment variables (when used without arguments) or run a command with a modified environment.

set (shell built-in): Displays all shell variables (including environment variables), shell functions, and positional parameters. Its primary purpose is to set or unset shell options.

COMMON USAGE

To list all environment variables:
printenv

To display a specific variable, for example, HOME:
printenv HOME

To check if a variable exists in a script:
if printenv MY_VAR > /dev/null; then
  echo "MY_VAR is set to: $(printenv MY_VAR)"
else
  echo "MY_VAR is not set."
fi

HISTORY

The printenv utility is a standard component of the GNU Core Utilities, a collection of essential command-line tools for Unix-like operating systems. Its functionality is fundamental to understanding and managing the process environment, and similar capabilities have been present in various forms across Unix systems since early days. Its development follows the general evolution of standard Unix commands towards the GNU/Linux ecosystem.

SEE ALSO

env(1), export(1), set(1), bash(1)

Copied to clipboard