Categories: How-To

Env command on Linux (runs a program in a modified environment) [Basic Guide]

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 
#! /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

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.