How to Use the env Command in Linux

Wondering How to Use the env Command in Linux?

env is a shell command used in Linux, and it stands for environment variables. An environment variable is a value named dynamically, and it can influence the way ongoing processes behave on a computer. 

Therefore, the env command is used to print a list of these environment variables. Similarly, the command is used to execute a utility in a modified environment without changing the currently existing conditions. In addition, it is used by shell scripts to initiate the correct interpreter. However, the environment here is not changed. 

In this article, we will learn how to use the env command in Linux, its options, the common environment variables, and also the various env command exit statuses.

Let’s dive right in!

Usage.

Firstly, let’s note that the general syntax for the env command is as shown below. It sets each NAME to VALUE and runs the COMMAND in that environment.

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

This command can run without any other argument, as shown in the syntax below:

$ env

As a result, it will display a list of all the current environment variables, as shown by the screenshot below.

env command in linux

Secondly, environment variables are case-sensitive. That is to say that they should have UPPER CASE names. Have a look at the examples below.

LOG=value
HOME="Yet another value here"
KEY=value_a:value_b

In addition, a colon “:” is used to separate multiple values assigned to a variable. Similarly, there is no space before or after the equals “=” sign. See the examples shown above to illustrate this.



Options of the env Command.

As mentioned above, the env command can be run without other parameters. On the other hand, here we will look at examples where the env command has arguments to it.

-i, or --ignore-environment.

This option enables us to start with an empty environment. That is to say, it clears the environment but still allows new variables to be set. 

Here is the syntax for this option: 

$ env -i command_to_ignore

This is the command we have used on the Command Terminal.

$ env -i /bin/sh

Below is the output displayed. As we can see, the environment has been cleared. That is to say that when we run the command env, the only environment variable that is available is PWD.

env command in linux

-0, or --null.

This option ends the outputs with a NULL value instead of a new line. By default, the results of the env command are usually printed on a new line. 

Here the syntax is shown below:

$ env -0
$ env --null 

Here is how the output looks like for this case:

env command in linux

-u, or --unset=NAME.

This option removes a variable from the environment. In other words, it removes the variable NAME hence it’s not seen on the output. 

This is the syntax for the parameter followed by an example with an actual variable:

$ env -u variable_to_unset
$ env -u LOGPATH

--version.

This option usually displays the version information and then exits. 

Here is the syntax for the command:

$ env --version

Have a look at the output below:

env command in linux

--help.

This option displays the help information, and then it exits. 

Here is the syntax for the command:

$ env --help

The output is as shown below:

env command in linux

env and hash-pling.

env can also be used in the hash-pling line of a script. This allows the interpreter to be searched through the PATH variable.

Here is an example of a Python file with (.py) extension and the hash-pling, which usually starts with (#!)

index.py:
#!/var/usr/env python2
print "Hello world. Python emphasizes code readability by using a lot of code indentation. It uses an object-oriented approach."

Here is the output for the command:

# python index.py
Hello world. Python emphasizes code readability by using a lot of code indentation. It uses an object-oriented approach.



Common Environment Variables.

The environment variables are output when you run the env command. As demonstrated above, the variables are in UPPER CASE form. Here are some of the common environment variables and their descriptions. 

HOME – This variable displays the home directory of the current user.

EDITOR – This variable prints the default file editor being used. In other words, this is the editor used on Command Terminal to run the commands.

USER – This variable shows the user that is currently logged in.

SHELL – This shows the location of the current user’s shell program. For example, bash, which is a command processor where a user runs their commands.

LANG – This displays the current locales settings.

LOGNAME – This is the name of the current user that’s logged in.

PATH – This shows a list of directories to be searched when executing commands. That is to say when a command is run, the system will look for these directories and use the first that it finds to execute.

TERM – This usually shows the current terminal emulation.

MANPATH – This shows the location of your manuals.

MAIL – This prints the location of where the mail of the current user is stored.

TZ – This displays the time zone used by the system.


Exit Status.

When the env command is run, it produces an exit code. This helps us to know the status of the command specified, based on the code produced. 

Below are the exit codes produced by the env command in Linux and their descriptions:

Exit CodeDescription
0This means the env command was completed successfully.
1-125This means an error occurred or the env command failed.
126This means the specified command was found but could not be invoked.
127This means the command specified was not found.

To sum up, we hope you have learned what the env command in Linux is, the common environment variables, and also its options. 

There are many more commands used in Linux. If you’d like to know how to extract your directory path in Linux, have a look at How to Use the dirname Command in Linux.

If you have any questions or suggestions on How to Use the env Command in Linux, let us know in the comment section below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post

How to Fix Error 5000 on Tinder

Next Post

How to Fix Bumble App Not Working

Related Posts