Linux Mint

How to Use env Command in Linux – Examples

env is the Linux command used to print the list of environmental variables. Through the env command, you can run another utility in a customized environment without changing the existing environment. Using this command, the environment variable can be added, deleted, the existing variables can be changed or you can also assign the values to them. In this tutorial, we will discuss environmental variables in Linux and how to create them.

What are Environmental Variables

The environmental variables are certain to the specific environment and each user in the OS has its own environment. Below are some examples of environmental variables in Linux:

  • USER: currently logged-in user
  • LANG: current language of the system
  • HOME: directory of the current user
  • SHELL: stores the path of current user shell
  • PATH: show list of directories
  • TERM: current terminal emulation

How to Use env Command in Linux – Examples

env command is used for different purposes, the general syntax of the command to display multiple variables is:

env [OPTION]... [-] [Name=Value ]... [Command [ Argument]... ]

 

The general syntax for displaying the single variable using the env command is:

env [Name]

 

Example 1: Print the set of a current variable using the following command:

env

 

Example 2: The null with the env command ends the output with the null instead of the new line:

env --null

 

Example 3: Using the printenv command you can display the value of individual variables:

printenv <variable-name>

 

To display the value of environmental variable HOME:

printenv HOME

 

How to Create a New Environmental Variable Using env in Linux

To create a new environment variable, use the following command:

export VARIABLE_NAME=value/path

 

Let’s create a new variable with the name JAVA_ENV:

export JAVA_ENV=/usr/bin/java

 

Use the echo $JAVA_ENV to verify the creation of the variable.

How to Remove Environment Variable via env in Linux

If you want to delete the created variable then use the following syntax:

env -u <variable_name>

 

For detailed information and usage of the env command execute the help command of it:

env --help

 

Bottom Line

The env command is used to either print the environmental variable or to create environment variables and manage them. It can be used with different flags and parameters and if they are not specified it simply prints the list of all current variables. This command also launches the correct interpreter in the shell scripts. We have demonstrated the creation of variables, displaying the current variables, and how to remove the create variable from the list in the above section of the guide.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.