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 [VARIABLE]
printenv [--help]
printenv [--version]

PARAMETERS

VARIABLE
    The name of the environment variable to print. If not specified, all environment variables are printed.

--help
    Display help information and exit.

--version
    Display version information and exit.

DESCRIPTION

The printenv command in Linux is used to print all or specific environment variables to standard output. Environment variables are dynamic named values that can affect the way running processes will behave on a computer. They provide a way to configure the behavior of applications without modifying their code. They are globally accessible to all processes. Without any arguments, printenv displays a list of all currently defined environment variables, each on a separate line. When a single environment variable name is specified as an argument, printenv displays only the value of that variable. This command is often used in shell scripts or interactive shell sessions to inspect the current environment and verify that variables are set correctly. It is commonly used in debugging to verify that software installations and environment configurations are properly initialized. The command is a basic, but essential utility for system administrators and developers.

CAVEATS

The output of printenv might be truncated depending on the terminal settings or pipe used. The command does not provide options to format the output or filter based on complex criteria; for more advanced filtering, you might use grep or other shell utilities in conjunction with printenv.

EXAMPLES

To display the value of the HOME environment variable: printenv HOME.
To display all environment variables: printenv.

USAGE IN SCRIPTS

printenv is often used to check the presence of a specific variable.
EXAMPLE:
#!/bin/bash
if [ -z "$(printenv MY_VARIABLE)" ]; then
echo "MY_VARIABLE is not set."
else
echo "MY_VARIABLE is: $(printenv MY_VARIABLE)"
fi

HISTORY

The printenv command has been a standard utility in Unix-like operating systems for a long time. It is designed to be a simple and direct way to inspect the environment variables, a critical aspect of process configuration and system setup. Its functionality is fundamental, and it has remained relatively unchanged over its history, reflecting its core purpose.

SEE ALSO

env(1), set(1), unset(1), export(1)

Copied to clipboard