Beginners Guide for Env Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The env command is an advanced version of printenv with a few more features to tweak your environment variables, like setting new environment variables, printing all environment variables, or executing a command or script in a modified environment.

I would also suggest you read the history of the env and printenv commands on this stack exchange answer.

Tutorial Details

DescriptionEnv
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesenv
Internet RequiredNo

Syntax of the env Command

The env command takes three arguments: one is the flag, another is the environment variable, and the third is the command or program name, and all are optional.

$ env [OPTION] [VARIABLE] [PROGRAM]

Display All of the Environment Variables

The following command, without any flag or variable name (key), will print the complete list of environment variables on your system.

$ env

Output:

Env command output without option

Display Only the Key of All Environment Variables

Instead of listing the environment variables keys with their values, you can pipe the awk command with env to print only the environment variables keys.

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

Output:

Extracting only key from the env command output

Display Each Key=Value with Null Instead of Newlines

Use the “-0” or “--null” flag to use null instead of newline while printing all the environment variables.

$ env -0

Output:

Env command output without new line

Custom Script File for Env Command

Further, you will learn how to use the env command to create a modified environment for your shell script.

For that, you can create a new script file using your choice of text editor (either Vim or Nano) and copy the following content into it.

echo "Username:" $USER
echo "Home Directory:" $HOME
echo "Default Shell:" $SHELL

Save and close the script with the “script.sh” name and give it executable permissions using the chmod command.

$ chmod +x script.sh

That’s all you have to do; now, for your knowledge, the commands written in this file will print the existing user’s username, home directory, and default shell as shown.

$ ./script.sh 

Output:

Creating and running script to demonstrate env command usage

So, let’s learn how to create a modified environment for this script, starting with

Clearing the System Environment Variable and Running the Script

By default, the script takes the value from your system environment variables. However, you can clear the complete system environment variables specific to your script using the “-i” or “--ignore-environment” flag.

$ env -i ./script.sh 

Output:

Flushing values picked from system in env script

As you can see above, all the values fetched by the system environment variables keys are cleared.

Removing the Specific Environment Variable and Running the Script

Previously, you cleared all the system environment variables, but if you want to remove only specific environment variables for your script, specify its variable name (key) to the “-u” or “--unset” flag.

$ env -u USER ./script.sh 

Output:

Removing value attached to specific key

As you can see, only the “USER” environment variable is removed for this script.

If you want to remove multiple environment variables, then specify the flag multiple times with the variable name (key) as shown.

$ env -u USER -u HOME ./script.sh 

Output:

Removing value attached to multiple keys

The above command will remove both the “USER” and “HOME” environment variables for the specified script.

Setting a Specific Variable and Running a Program

Instead of removing all or a specific environment variable, if you want to replace the single existing environment variable, then specify the existing variable key with a new value like (key=value) as shown.

$ env USER=jake ./script.sh 

Output:

Setting custom value to key specificed in script

As you can see above, instead of picking the system environment variable, it replaced the “USER” variable with a custom “jake” value.

If you want to replace multiple variable values, then specify each of them with a space.

$ env USER=jake HOME=/home/jake ./script.sh 

The above command will replace the values for both the “USER” and “HOME” environment variables.

Setting custom value to multiple keys in script

Loading Environment Variables from the Env File

Instead of specifying each custom variable key and value to the env command, you can create a file and save all the environment variables with their custom values in that file, as shown.

USER=jake
HOME=/home/jake
SHELL=/bin/zsh

The above file will modify the existing “USER“, “HOME“, and “SHELL” variable values.

To create a modified environment for your script using this file, you can source the file with the source command and execute it by specifying your script to the bash command, as shown.

$ (source 'file.env' && bash 'script.sh') 

Output:

Altering env script keys value from different file

As you can see, instead of picking the variables’ values from the system, it uses the “file.env” file environment variables’ values.

That’s all about the env command.

If you have any questions related to this topic, feel free to ask them 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.