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.
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!
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:
As a result, it will display a list of all the current environment variables, as shown by the screenshot below.
Secondly, environment variables are case-sensitive. That is to say that they should have UPPER CASE names. Have a look at the examples below.
HOME="Yet another value here"
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.
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.
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:
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
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:
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 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 (#!)
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.
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:
|This means the env command was completed successfully.
|This means an error occurred or the env command failed.
|This means the specified command was found but could not be invoked.
|This 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. 🙂