Linux env Command Tutorial For Beginners (5 Examples)

The Linux env command is used to display and manage the environment variables in a shell session. Environment variables are dynamic values that affect the processes or programs running in the shell, such as paths to executable files, user-specific settings, and system behavior. By running the env command without arguments, it lists all current environment variables and their values. Additionally, env can be used to execute a command with a modified environment by temporarily setting or overriding specific environment variables for that command's duration. This is useful for testing or running programs in a specific environment without altering the global configuration.

But before we do that, it's worth mentioning that all the commands and instructions in this tutorial have been tested on Debian 12 and Ubuntu 24.04.

Linux env command

By definition, the env command lets you run a program in modified environment. Following is the syntax of the command, as mentioned on its 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.

Following Q&A-styled examples should give you a better idea on how this command works:

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

Bash environment, as you might already know, consists of VARNAME=VALUE entries. To access all environment variables as well as the values associated with them, execute the env command without any option.

env

Here's the output of the above command in our case:

How to access all environment variables using env command

Q2. How to temporarily change environment using env?

The key feature of env is the ability to temporarily change a process's environment. For example, we created a small executable — dubbed env — that displays the value of the USER environment variable upon execution.

Here's the output in the normal scenario:

How to temporarily change environment using env

Now, what we did was, we used the env command to temporarily change the value of USER environment variable from 'himanshu' to 'HTF' for the executable/process. Following is the command we used in this case:

env USER=HTF ./env

And here's the output produced in this case:

env command output

So you can see that the executable returned the new value.

Note: As suggested by the tool's generic syntax, you can tweak values of multiple environment variables and make the process use these new values.

Q3. How to make a process ignore existing environment using env?

If you want, you can also make a process ignore existing/inherited environment, and start with an empty one instead. This can be done using the -i or --ignore-environment option.

For example:

How to make a process ignore existing environment using env

Q4. How make env use NUL instead of newline character in output?

In the first example we discussed above, the output lines produced by env are separated by newline. However, if you want, you can make env use the NUL character as separator. This feature can be accessed using the --null command line option.

env --null

Following is an example screenshot;

How make env use NUL instead of newline character in output

Q5. How to know the error based on env command exit status?

The env command produces the following exit codes: 0, 125, 126, and 127. Following are the associated error descriptions:

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

If you get an error code other than the ones mentioned above, that's the exit status returned by the process/command that was executed with a modified environment.

Conclusion

If you are a complete command line newbie, chances are that you won't be requiring this tool on a daily basis. However, that's not to say it's not worth knowing about - in fact, env comes in really handy in many situations. The examples we've shared in this tutorial should be enough to give you a head start. For more information, head to the command's man page, or better, execute the following command:

info coreutils env invocation
Share this page:

0 Comment(s)