env Command in Linux



env is a Linux command that is used to display or modify environment variables of the current shell session. The environment variables on the other hand are dynamic values that can affect the behavior of the processes running on your system.

By executing the env command without an argument, you will be able to view the list of all current environment variables on your system. Apart from that, you can also use the env command to run a command with a modified environment, without changing the current one. This practice is pretty much useful in scripts to ensure the correct interpreter is used.

Table of Contents

Here is a comprehensive guide to the options available with the env command −

Syntax of env Command

The env command in Linux is a handy tool for managing environment variables. Here’s a simple breakdown of its syntax −

env [options] [NAME=value]... [command [args]...]

Here,

  • [options] are flags used to change the command’s behavior.
  • [NAME=value] is the set environment variables for the command.
  • [command [args]...] is the command to run with the specified environment.

env Command Options

With env command on Linux, you can use different options, these are discussed in the table provided below −

Option Description
-0 or --null Ends each output line with a null character instead of a newline, useful for processing output with xargs -0.
-C or --chdir=DIR Changes the working directory to the specified DIR before running the command.
-i or --ignore-environment Runs the command with an empty environment, ignoring all existing variables.
-S or --split-string=S Splits the string S into separate arguments, useful for passing multiple arguments in scripts.
-u or --unset=NAME Removes the specified environment variable before running the command.
--help Displays a help message with a summary of options and exits.
--version Shows the version information of the env command and exits.
-v or --debug Prints verbose information for each processing step, useful for debugging.

Examples of env Command in Linux

Let’s explore a few examples of env command in Linux, which are as follows −

  • View All Environment Variables
  • Run a Command with a Modified Environment
  • Ignore the Current Environment
  • Unset an Environment Variable
  • Change the Working Directory

View All Environment Variables

You can use the env command on Linux without an argument to display all current environment variables, as given below −

env

The above command will list all the environment variables set in your current session −

View All Environment Variables

Run a Command with a Modified Environment

You can also run a command with a specific environment variable set by using the env command followed by the variable and the command. For example, to run a script with a custom variable, you can consider the following command −

env MY_VAR=hello ./script.sh
Run a Command with a Modified Environment

Note − You may use sudo with the env command in some cases if you are login to your Linux system as a non root user.

Ignore the Current Environment

If you want to run a command with an empty environment, ignoring all existing variables, you can use the -i option with the env command. This is useful for testing how a command behaves without any environment variables, for example −

env -i command
Ignore the Current Environment

Unset an Environment Variable

You can also remove a specific environment variable before running a command by using the -u option with the env command followed by the variable name. For example, to unset the PATH variable, you can use the below-given command −

env -u PATH ./script.sh
Unset an Environment Variable

Change the Working Directory

If you want to change the working directory before running a command, you can simply use the -C option with the env command and the directory path in the end. This runs the command in the specified directory −

env -C /new/directory command
Change the Working Directory

In this way, you can use the env command on your Linux system.

Conclusion

The env command is a powerful tool used in Linux for managing environment variables. By using the env command, you can view all current environment variables, or run commands with a modified environment without altering the existing one.

In this tutorial, we explained the env command, its basic syntax, followed by different options that you can use with the command. Further, examples are explored in the end to help users understand the env command usage. This will help enhance their ability to manage and troubleshoot environment settings in an effective manner.

Advertisements