Linux env command tutorial for beginners (5 examples)

If your job involves doing things from the command line on Linux, then chances are you will be using the Bash shell. Did you know that there is an environment associated with processes that run through the shell? And, if you want, you can customize this environment specifically for this task. In this article, we will look at the command line utility env, it not only allows you to access the shell environment, but also allows you to make changes to it as needed.

But before we do that, it’s worth noting that all the commands and instructions mentioned in this tutorial have been tested on a Bash shell running on Ubuntu 16.04 LTS.

Linux env command

By definition, the env command allows you to run a program in a modified environment. Following is the syntax of the command, as mentioned in the man page:

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

And here’s what the man page says about env:

Set each NAME to VALUE in the environment and run COMMAND.

The examples should give you a better idea of ​​how this command works:

Q1. How to access all environment variables using the env command?

The Bash environment, as you already know, consists of VARNAME = VALUE entries. To access all environment variables and the values ​​associated with them, run the env command with no option.


Here is the output from the above commands in our case:

[email protected]:~# env                                                                                                                                          
HISTTIMEFORMAT=%h %d %H:%M:%S                                                                                                                                 

Q2. How to temporarily change the environment using env?

A key feature of env offers the ability to temporarily change the conditions for a process. For example, we have created a small executable process that displays the value of the USER environment variable at runtime.

Here’s the output in a typical script:

[email protected]: ~/htf-dayly$ ./env
USER: destroyer

Now what we have done, we have used the ENV command to temporarily change the value of the USER environment variable from “destroyer” to “HTF” for the executable process. Below is the command we used in this case:

env USER=HTF ./env

But the exit is made in this case:

[email protected]: ~/htf-dayly$ env USR=HTF ./env

So you can see that the executable is returning a new value.

Note: As suggested by the general syntax of the tool, you can change the values ​​of several environment variables and make the process of using these new values.

Q3. How do I make the process ignore the existing environment using env?

If you want, you can also have the process ignore the existing / legacy environment, and start from empty instead. This can be done using option -i or –Ignore-environment

For example:

[email protected]: ~/htf-dayly$ env -i ./env
USER: SORRY, No value

Q4. How do I use the env command to use NUL instead of the newline character on exit?

In the first example above, lines were output from the env command, separated by a newline character. However, if you want, you can make env use the NUL character as a delimiter. This feature can be accessed using the option –Null at the command line.

env --null

Below is a sample screen:

[email protected]:~# env --null                                                                                                                                   
HLVL=1HOME=/rootLANGUAGE=en_US:LOGNAME=destroyerHISTTIMEFORMAT=%h %d %H:%M:%S _=/usr/bin/[email protected]:~#                                                  

Q5. How do I know the error based on the state of the env output command?

The env command produces the following output codes: 0, 125, 126 and 127. The following are descriptions of the errors associated with them:

0 if no COMMAND is specified and the environment is output
125 if ‘env’ itself fails
126 if COMMAND is found but cannot be invoked
127 if COMMAND cannot be found

In this case, if you receive an error code other than those mentioned above, then this output state is returned by a process / command that was executed in a modified environment.


If you’re a complete command line newbie, chances are you won’t be requiring this tool on a daily basis. However, that doesn’t mean it’s not worth knowing – in fact, env helps in a lot of situations. The examples we’ve shared in this article are enough to give you an understanding of the command. For more information, use the man page, or better, run the following command:

info coreutils env invocation