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
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Print one value
printenv HOME /home/studentOnly the value, no name. Easy to use in scripts.
2Print several values
printenv USER SHELL LANG student /bin/bash en_US.UTF-8One line per variable, in the order you asked.
3Print all environment variables
printenv | sortSame list as
env, sorted by name.4Check if a variable exists
printenv KUBECONFIG >/dev/null || echo "KUBECONFIG is not set"The exit code is 1 when the variable is missing.
5Make a script stop when a needed variable is missing
Fail early with a clear message, before the script touches anything.
6Check the proxy settings
printenv | grep -i _proxyWrong proxy variables are a common reason why downloads fail on servers.
7See the PATH one folder per line
printenv PATH | tr ':' '\n'Easy to see a wrong or missing folder.
8Compare a variable as your user and with sudo
printenv PATH; sudo printenv PATHsudo uses its own safe PATH (from
secure_pathin sudoers).9Check what a systemd service gets (test a unit)
systemd-run --user --wait --pipe printenv | headRuns printenv the way a service would run, so you see its environment.
10Print a variable that a CI job sets
printenv CI_COMMIT_SHACI systems pass build data in environment variables; print them to debug a pipeline step.
11Print values with NUL separators
printenv -0 USER HOME | xargs -0 -n1 echoSafe for values that contain spaces or new lines.
Nothing matches this filter.
Watch out
- Do not write
$before the name:printenv HOME, notprintenv $HOME. - Not exported = not printed. Use
echo "$NAME"orsetfor shell variables.
Tips to remember
printenv NAME= value only.env= all.echo $NAME= also non-exported variables.