Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

printenv

Print the value of environment variables (all, or the ones you name).

Level
Everyday Linux
Group
Shell and help
Needs root
No
Package
coreutils

What it does

printenv without arguments prints every environment variable (like env). With names, it prints only the values, one per line, and returns an error code when a variable does not exist. That makes it handy in scripts and checks.

It only shows exported variables. A shell variable that was not exported is not printed.

How to write it

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

Options 4

Most used

VARIABLE
Print only the value of this variable. Several names are allowed.
-0, --null
End each output line with a NUL byte instead of a new line.
--help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Print one value

    printenv HOME
    /home/student

    Only the value, no name. Easy to use in scripts.

  2. 2Print several values

    printenv USER SHELL LANG
    student
    /bin/bash
    en_US.UTF-8

    One line per variable, in the order you asked.

  3. 3Print all environment variables

    printenv | sort

    Same list as env, sorted by name.

  4. 4Check if a variable exists

    printenv KUBECONFIG >/dev/null || echo "KUBECONFIG is not set"

    The exit code is 1 when the variable is missing.

  5. 5Make a script stop when a needed variable is missing

    for v in DB_HOST DB_USER; do printenv "$v" >/dev/null || { echo "missing $v"; exit 1; }; done

    Fail early with a clear message, before the script touches anything.

  6. 6Check the proxy settings

    printenv | grep -i _proxy

    Wrong proxy variables are a common reason why downloads fail on servers.

  7. 7See the PATH one folder per line

    printenv PATH | tr ':' '\n'

    Easy to see a wrong or missing folder.

  8. 8Compare a variable as your user and with sudo

    printenv PATH; sudo printenv PATH

    sudo uses its own safe PATH (from secure_path in sudoers).

  9. 9Check what a systemd service gets (test a unit)

    systemd-run --user --wait --pipe printenv | head

    Runs printenv the way a service would run, so you see its environment.

  10. 10Print a variable that a CI job sets

    printenv CI_COMMIT_SHA

    CI systems pass build data in environment variables; print them to debug a pipeline step.

  11. 11Print values with NUL separators

    printenv -0 USER HOME | xargs -0 -n1 echo

    Safe for values that contain spaces or new lines.

Watch out

  • Do not write $ before the name: printenv HOME, not printenv $HOME.
  • Not exported = not printed. Use echo "$NAME" or set for shell variables.

Tips to remember

  • printenv NAME = value only. env = all. echo $NAME = also non-exported variables.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man printenv