Skip to content

What is printenv Linux command?

The printenv command in Linux is used to display the current environment variables. It prints the values of the specified environment variables or lists all environment variables if no argument is provided. It is commonly used to check the environment variables set for a particular session or script.

printenv Syntax:

Terminal window
printenv [option] [parameter]

printenv Options:

OptionDescription
-0Delimit output with null
-hDisplay help information
-vDisplay version information

Parameters:

ParameterDescription
nameName of the environment variable

printenv Command Usage Examples:

Display All Environment Variables

Terminal window
printenv

Displays all current environment variables.

Display the PATH Environment Variable

Terminal window
printenv PATH

Displays the value of the PATH environment variable.

Display a Specific Environment Variable

Terminal window
printenv USER

Displays the value of the USER environment variable.

Display the SHELL Environment Variable

Terminal window
printenv SHELL

Displays the value of the SHELL environment variable.

Use printenv with Grep to Filter Output

Terminal window
printenv | grep HOME

Uses printenv with grep to filter the output for environment variables containing “HOME”.

How do I use printenv in Linux?

To use the printenv command in bash, execute the following command:

Terminal window
printenv

How can I view only specific environment variables with printenv?

To view specific environment variables using printenv, specify the variable names as arguments. For example:

Terminal window
printenv PATH USER HOME

How can I search for a specific variable with printenv?

To search for a specific environment variable using printenv, you can pipe the output to the grep command. For instance:

Terminal window
printenv | grep USER

How can I redirect the output of printenv to a file?

To save the output of printenv to a file, you can use output redirection. Here is an example:

Terminal window
printenv > environment_variables.txt

How can I sort the output of printenv alphabetically?

To sort the output of printenv alphabetically, you can pipe the output to the sort command. Here is how you can do it:

Terminal window
printenv | sort

How can I display the values of variables that are currently set in the environment with printenv?

To display the values of variables currently set in the environment using printenv, you can pipe the output to the cut command. Here’s an example:

Terminal window
printenv | cut -d= -f2

How can I unset or remove a specific environment variable with printenv?

To unset or remove a specific environment variable using printenv, you need to use the unset command in conjunction with the variable name. For example:

Terminal window
unset VARIABLE_NAME

How can I display environment variables in a specific format with printenv?

To format the output of printenv in a specific way, you can use tools like awk or sed to manipulate the output. Here is an example:

Terminal window
printenv | awk -F= '{print "Variable: " $1, "Value: " $2}'

Applications of the printenv command

  • Displaying all environment variables
  • Checking the value of a specific environment variable
  • Use in shell scripts to access and use environment variables