Skip to content

Env command on Linux (runs a program in a modified environment)

The env command in Linux is used to execute a program in a modified environment by sending it one or more environmental variables. It is very useful for running software tests.

It enables a given program to read a variable without the need to create the variable in the Shell and then export it with the export command.

The -i option tells the env to ignore the inherited environment without altering the content of any existing variable. It is useful for modifying a variable momentarily for a test.

In this example the programx is executed by receiving the HOME variable with its value temporarily and individually changed:

$ echo $HOME /home/uiraribeiro $ env HOME=/home/guest2 programs

But when viewing the value of the HOME variable immediately after the program is executed, it appears that its content remains unchanged, since it was only modified in the memory space that the programax was able to see:

$ echo $HOME /home/uiraribeiro

The env command can also be used with the “-u” option, which removes the specified variable from the environment. To demonstrate, we created a short script that prints the BOOK variable:

$ cat script <br></br>#! /bin/bash

echo “The book is: $BOOK” ``` $ chmod +x script

$ export book=“Linux certification”

$. /script

The book is: Linux Certification When using env with the -u option, the BOOK variable will cease to exist for the script when executed by the env:

$ env -u BOOK. /script

The book is: And it is also possible to use the -i option, which cleans all exported variables, running the program in a completely clean environment:

$ env -i. /script

The book is: Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
-----------------
Share