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
Description | Printenv (Print Specified or All Environment Variables) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | printenv |
Internet Required | No |
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:
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:
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:
Another example;
$ printenv PATH
Output:
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:
The following is with variable name:
$ printenv -0 PATH
Output:
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.