Beginners Guide for Printenv Command on Linux

Linux TLDR
Last Updated:
Reading time: 1 minutes

The printenv command is an alternative to the env command used to fetch the value of a specified variable name (key) used as an argument.

If no variable name is assigned to printenv, it will print all environment variables in your system.

Tutorial Details

DescriptionPrintenv (Print Specified or All Environment Variables)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesprintenv
Internet RequiredNo

Syntax of the Printenv Command

The printenv command takes two arguments: one is the option; the other is the variable name (key) and both are optional.

$ printenv [OPTION] [VARIABLE-NAME]

Display Key-Value Pairs of All Environment Variables

The following command, without any options or keys, will print the complete list of environment variables (key-value pairs) in your system.

$ printenv

Output:

Listing all env variables using printenv command

Display Only Keys for All Environment Variables

Instead of values, if you want to print all the environment variables keys in your system, pipe the command with the awk command.

$ printenv | awk -F "=" '{print $1}'

Output:

Listing only env variables keys using printenv command

Display the Value of a Specific Variable Key

If you want the value of a specific variable, assign its variable name (key) to the printenv command as shown.

$ printenv HOME

Output:

Printing HOME variable value using printenv command

Another example;

$ printenv PATH

Output:

Printing PATH variable value using printenv command

Display the Value of the Variable without Newlines

When you execute any of the above commands, it will print the variable value in newlines.

This can be avoided with NULL by using the “-0” or “–null” flag.

The following is without any variable names:

$ printenv -0

Output:

Listing all env variables in same line using printenv command

The following is with variable name:

$ printenv -0 PATH

Output:

Printing PATH variable value in same line using printenv command

That was all there was to this basic command.

If you have any questions, feel free to ask us in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.