Linux printenv Command: Print Environment Variable Values

printenv is a GNU Coreutils command that prints the values of the variables in the current process environment. If you specify a variable name, you can check only the needed value, and if you omit the name, it prints all environment variables.

Unlike env, which changes the environment to run a command, printenv focuses on inspection. It is useful for checking the difference between shell variables and environment variables or confirming whether a variable has been passed in a script.

What is the printenv command?

A program receives a set of environment variables from its parent process. printenv sends either the entire set or the value of a specified name from the environment it was given to the standard output.

Variables that exist only inside the shell and have not been exported are not included in the environment, so they will not be visible with printenv.

Basic Syntax

printenv [options] [variable...]

When run without a variable name, it prints all environment variables in the name=value format.

printenv
printenv | sort

The full environment can include authentication tokens or service access information. Before sharing the results or saving them to a log, make sure there are no sensitive items.

Checking a specific environment variable

printenv HOME
printenv PATH
printenv LANG

If you specify a variable name, only the value is output, and the name= prefix is not attached. You can also specify multiple names at once.

printenv HOME SHELL USER

Only with the output, it may be difficult to distinguish which variable each line corresponds to, so if it is a report for humans, consider a way to display the name separately as well.

Checking whether a variable exists by exit status

GNU printenv returns 0 if all specified variables are found, 1 if any cannot be found, and 2 if a write error occurs.

if printenv APP_ENV >/dev/null; then
  printf '%s\n' 'APP_ENV is exported'
else
  printf '%s\n' 'APP_ENV is not exported'
fi

An environment variable with an empty string value is also a "variable that exists." To distinguish between an empty value and the absence of a variable, you need to check both the exit status and the output together.

Cases where there is a shell variable but it does not appear in printenv

APP_MODE=development
printf '%s\n' "$APP_MODE"
printenv APP_MODE

The first assignment creates a Bash shell variable, but it is not exported to child process environments. printenv is an external program, so it cannot receive that value.

export APP_MODE
printenv APP_MODE

After export, printenv and other child processes executed afterward can receive the value.

Viewing PATH entries line by line

PATH is a list of directories separated by colons, so you can split it like this to make it easier to read.

printenv PATH | tr ':' '\n'

Empty entries can represent the current directory, which may be a security issue. Check if there are colons at the beginning, end, or consecutive colons in the output as well.

Using -0 to separate with NUL characters

printenv -0
printenv --null

-0 or --null outputs NUL characters at the end of each item instead of newlines. Since the environment variable values themselves may contain newlines, this is useful when another program needs to handle the results unambiguously.

printenv -0 | while IFS= read -r -d '' item; do
  printf '%q\n' "$item"
done

The read -d usage in this example is based on Bash. Simple text tools may not support NUL-separated input, so ensure the entire pipeline can handle this format.

Supported options

Option Description
-0, --null End each output item with a NUL character instead of a newline.
--help Prints the usage and exits.
--version Prints the program version and exits.

-0 is a feature of GNU Coreutils. Support may vary on other Unix-like systems.

Comparison of printenv, env, echo

Command Items to Check Features
printenv NAME Environment variables passed to child processes Useful for checking the existence of a specific environment variable in the exit status.
env The entire current environment The core feature is changing the environment to execute another command.
printf '%s\n' "$NAME" Current value of the variable expanded by the shell You can also display shell variables that have not been exported to the environment.

You can also see the value with echo "$NAME", but in scripts, printf is more predictable to avoid differences in implementation such as values that look like options or backslash handling.

Checking temporarily set environment

When used with env, you can check the values that will be passed to a specific command.

env APP_ENV=staging printenv APP_ENV
env -u HTTP_PROXY printenv HTTP_PROXY

The second command outputs nothing and returns a failure status because the variable has been removed.

Why values differ for each process

The environment is not a single table for the entire system but data that each process has. A new terminal, SSH session, systemd service, cron job, and container use different parent processes and startup settings, so the same variable name may have a different value or may not exist.

When diagnosing problems, you should run printenv in the same user, service, and container environment in which the actual program is running. Do not assume that values visible in the terminal are automatically passed to the service.

Security precautions when displaying environment variables

  • Do not paste the full printenv results in public issues or chats.
  • Check whether tokens and passwords are masked in CI logs.
  • Only specify the variable names you need to check the minimum number of values.
  • When checking whether a secret value is needed, do not print the value; use the exit status instead.
  • Environment variables can be exposed to debugging tools or child processes with the same permissions, so they should not be considered a secret store in themselves.

Common issues

The value may not appear even if it has been set.

It is likely that you only created a shell variable without export-ing it. Execute export NAME and then check in a new child process.

An empty line was printed, does that mean the variable doesn’t exist?

It can be difficult to distinguish just by looking at the screen whether a variable is set to an empty string or doesn’t exist. Check the exit status of the command.

printenv EMPTY_VALUE
printf 'status=%s\n' "$?"

The environment variable disappears after sudo.

sudo removes or restricts environment variables according to security policies. Rather than bypassing to always preserve them, review the sudoers policy and only the variables necessary for the target command.

Frequently Asked Questions

Does printenv show all the variables of the current shell?

No. It only shows the variables that have been exported to the environment. Bash’s local variables, arrays, and some special variables need to be checked with shell built-in commands like declare or set.

Can I print the name and value of a specific variable together?

printenv NAME only prints the value. If you need the name as well, you can structure it like printf 'NAME=%s\n' "$(printenv NAME)", but be careful for tasks where you need to preserve the original value exactly, as command substitution removes the trailing newline.

Can you change environment variables with printenv?

No. printenv is a query command. In the current shell, use assignment and export, and to apply to only one command, use env NAME=VALUE command.

Related articles and official documentation

More in This Category
Linux tail Command: View the End of a File and Follow Logs

Linux tail Command: View the End of a File and Follow Logs

Learn how to use the Linux tail command to view the last lines of a file and follow log updates in real time, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux killall Command: Signal Processes by Name

Linux killall Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux killall command, including practical examples, key options, and important precautions.

Linux wc Command: Count Lines, Words, Characters, and Bytes

Linux wc Command: Count Lines, Words, Characters, and Bytes

Learn how to use the Linux wc command to count lines, words, characters, and bytes in files or standard input, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux watch Command: Run a Command Repeatedly

Linux watch Command: Run a Command Repeatedly

Learn how to use the Linux watch command to rerun a command at regular intervals and highlight changing output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux shutdown Command: Schedule a Shutdown or Restart

Linux shutdown Command: Schedule a Shutdown or Restart

Learn how to schedule a Shutdown or Restart with the Linux shutdown command, including practical examples, key options, and important precautions.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.