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. 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. 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: This is the command we have used on the Command Terminal. 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.
-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: Here is how the output looks like for this case:
-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:
–version.
This option usually displays the version information and then exits. Here is the syntax for the command: Have a look at the output below:
–help.
This option displays the help information, and then it exits. Here is the syntax for the command: 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 (#!) Here is the output for the command:
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: 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. 🙂