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:
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:
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:
echo “The book is: $BOOK” ```
$ chmod +x script
$ export book=“Linux certification”
$. /script
$ env -u BOOK. /script
$ env -i. /script