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
Description | Env |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | env |
Internet Required | No |
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:
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:
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:
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:
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:
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:
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:
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:
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.
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:
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.