Beginners Guide for Printenv Command in 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.

Also Read: How to Set and List Environment Variables in Linux

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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Internet RequiredNo
Discussed Tools in this Article

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.


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

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}'

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

Another example;

$ printenv PATH

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

The following is with variable name:

$ printenv -0 PATH

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.