Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Linux env command

Reference for the Linux env command: print the environment, run a program with extra or removed variables, start from an empty environment and use env in shebangs.

env does two things. Without arguments it prints the current environment, one NAME=value pair per line. With a command it runs that command in a modified copy of the environment: you can add variables, remove some, or throw everything away first. The change applies only to that one child process; your shell is not touched.

It is also the portable way to start an interpreter from a script: #!/usr/bin/env python3 finds the interpreter on PATH instead of hard-coding its location.

Terminal
env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]

Try it

Bash Adding, removing and clearing variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Splitting strings, NUL separators and exit codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
env [-i] [-0] [-u NAME]... [-C DIR] [-S STRING] [NAME=VALUE]... [COMMAND [ARG]...]
Name Description
NAME=VALUE Set NAME to VALUE in the child's environment.
-i, --ignore-environment, - Start with an empty environment instead of a copy of the current one.
-u NAME, --unset=NAME Remove NAME from the child's environment. Repeatable.
-0, --null Without a command, end each printed line with a NUL byte instead of a newline.
-C DIR, --chdir=DIR Change to DIR before running the command (GNU coreutils 8.28 and newer).
-S STRING, --split-string=STRING Split STRING into separate arguments, so that a single shebang argument can carry several options (8.30 and newer).
COMMAND Program to run. Found through PATH; shell builtins and functions are not available.

Returns: with a command, the command's exit status. If the command cannot be found, 127; if it exists but cannot be run, 126; if env itself fails (bad option), 125. Without a command, 0 after printing.

Usage notes

  • Only exported variables. env shows and passes on the environment, not every shell variable. A plain x=1 that was never exported does not appear. Compare with export and set.
  • env versus a prefix assignment. NAME=value command does the same job for a single command, built into the shell. env adds -i, -u and the ability to start a program from another program such as xargs or ssh, where no shell syntax is available.
  • env -i is harsh. With an empty environment there is no PATH; the shell used to look up command may still find it, but the program itself may fail to find helpers or its locale. Pass what is needed: env -i PATH=/usr/bin:/bin HOME="$HOME" cmd.
  • Shebang use. #!/usr/bin/env bash works on systems where Bash is not in /bin. Many kernels pass everything after the interpreter as one argument, so #!/usr/bin/env python3 -u fails; #!/usr/bin/env -S python3 -u splits it correctly.
  • Reproducible runs. env -i is a quick way to test whether a script relies on variables from your login environment, which is a common reason for "works in my terminal, fails in cron".
  • Sorting the output. Variable order is not guaranteed; pipe to sort for stable results, as above.
  • Multi-line values make the plain output ambiguous. Use -0 and a NUL-aware reader such as xargs -0 or read -d ''.
  • printenv NAME prints just the value of one variable, without the NAME=.