October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
Bash

How to Use the env Command in Linux

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Linux env command prints the environment it receives or launches a command with selected environment variables added, removed, or replaced. For a one-command setting, use env NAME=value command; the change applies to that child process, not to your current shell.

What does env do?

A process environment is a collection of strings conventionally written as NAME=value. Common names include PATH, HOME, USER, LANG, and EDITOR. When a process starts another process, the child normally receives a copy of the parent’s environment. The Linux environment documentation describes this representation and inheritance model.

PATH is a colon-separated list of directories searched for executable files when a command is given without a slash. env can pass a modified environment to a child, but it cannot reach backward and change the shell that started it.

Shell variables and exported variables

A shell variable is not automatically part of the environment inherited by child processes:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MESSAGE="hello"
env | grep '^MESSAGE='

This usually prints nothing. Exporting the variable makes it available to child processes:

export MESSAGE="hello"
env | grep '^MESSAGE='

Bash also supports a temporary assignment before a command, which affects that command’s environment rather than persisting as a shell-wide change. Bash’s manual documents command-specific assignments.

Basic syntax and quick choices

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

Options come first, followed by any assignments, then the command and its arguments. With no command, env prints the environment. The POSIX interface and GNU options are not identical; portability is covered below.

Goal Example
Print the environment env
Set a variable for one command env NAME=value command
Remove a variable for one command (GNU) env -u NAME command
Ignore the inherited environment env -i command
Start clean but retain essentials env -i PATH=/usr/bin:/bin HOME="$HOME" command
Change the child working directory (GNU) env -C /dir command
Run shell syntax env NAME=value sh -c '...'
Find an interpreter in a shebang #!/usr/bin/env python3

Display environment variables

Run env with no command to print the environment, one NAME=VALUE pair per line. This basic behavior is specified by the Linux env manual and the POSIX specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
env
env | sort
env | grep '^PATH='
env | grep -E '^(HOME|USER|SHELL)='

For a particular variable, printenv PATH prints its value; printf '%sn' "$PATH" prints the value held by your current shell. Treat a complete environment dump as sensitive: it may expose credentials, tokens, proxy configuration, or application settings. Redact it before sharing logs or bug reports.

Set a variable for one command

Put one or more assignments after env and before the command:

env MODE=development ./app
env APP_ENV=production LOG_LEVEL=info ./server

The launched program receives those values. A preceding shell assignment is often equivalent for a simple command:

MODE=development ./app

Choose the shell form when it is a straightforward temporary assignment. Choose env when its intent is clearer, when constructing a command, or when you also need environment options such as -i, -u, or GNU -C. POSIX notes that env also provides environment printing and environment replacement, so it is not merely another spelling of an assignment. See the POSIX env specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To check scope, launch a child shell, then inspect the parent shell afterward:

env DEMO=value sh -c 'printf "DEMO=%sn" "$DEMO"'
printf '%sn' "${DEMO-unset}"

The child prints DEMO=value; unless DEMO was already set in your shell, the second command prints unset.

Empty is not the same as unset

env FEATURE_FLAG= ./app passes a present variable with an empty value. env -u FEATURE_FLAG ./app removes it from the child environment. Applications can behave differently when a variable is absent, present but empty, or present with a nonempty value. GNU’s env documentation explicitly distinguishes an empty value from an unset variable.

Quoting and expansion

Your current shell expands variables before it starts env. In env NAME="$VALUE" command, $VALUE is expanded by the current shell; quoting it preserves spaces and shell-special characters in the resulting value. By contrast, env NAME='$VALUE' command passes the literal characters $VALUE in common shells.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If the child must interpret shell syntax or expand a variable, run a shell explicitly:

env NAME="$VALUE" sh -c 'printf "%sn" "$NAME"'

The outer shell supplies the value to env; the child shell expands $NAME when it runs.

Remove a variable with GNU -u

GNU env can omit one or more variables from the environment passed to a command:

env -u DEBUG ./app
env -u DEBUG -u API_URL ./app

The long form --unset=DEBUG is also available in GNU Coreutils. This does not unset the variable in your current shell. To test the child’s view, use env -u DEBUG sh -c 'env | grep "^DEBUG=" || echo "DEBUG is absent"'; the shell itself remains unchanged when that child exits. The Linux env manual documents -u.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start with a clean environment using -i

-i (or --ignore-environment) tells env to ignore the inherited environment and use the assignments supplied on its command line. POSIX defines this clean-environment behavior, subject to implementation requirements. POSIX env and the Linux manual describe the option.

env -i ./app

A program may need a search path, home directory, or locale, so a controlled environment is often more practical:

env -i 
  PATH=/usr/bin:/bin 
  HOME="$HOME" 
  LANG=C 
  ./app

Use env -i to check whether a program depends on accidental shell configuration, test startup behavior, or reduce environment differences in automation. Restore only variables the test needs. It is not a sandbox: it does not isolate files, users, network access, processes, capabilities, or the filesystem.

Why a clean environment can break command lookup

If you give env a command name without a slash, it searches for that command using PATH. With -i, no inherited PATH remains, so env -i sh may fail to find sh. Use a known absolute path for the target system, or supply a path explicitly:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
env -i PATH=/usr/bin:/bin sh -c 'command -v ls'

The supplied PATH also affects how programs launched by the child find their own commands. Avoid putting a writable current directory in PATH, since that can cause an unintended executable to be selected.

Change the child’s working directory with GNU -C

GNU env supports -C DIR (or --chdir=DIR) to change directory before starting the command:

env -C /tmp pwd
env -C /tmp sh -c 'pwd; printf "%sn" "$PWD"'

The change applies to the child process, not to your parent shell. -C is a GNU extension, not part of POSIX env. A portable shell pattern is a subshell:

(
  cd /var/tmp || exit
  exec ./app
)

Use env in a shebang

A shebang such as #!/usr/bin/env python3 asks env to find python3 through PATH, rather than assuming a fixed interpreter location. The script must also be executable, for example with chmod +x script.py, before you run ./script.py. The same pattern is commonly used for Bash, Perl, and Ruby.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This approach still depends on /usr/bin/env existing, the interpreter being installed, and PATH selecting the intended version. If its location is known and stable on the target system, an absolute interpreter path such as #!/usr/bin/python3 may be more predictable.

Passing multiple interpreter arguments

This form is unreliable on many systems:

#!/usr/bin/env perl -T -w

The system may pass everything after the first space as one argument, causing env to look for a program literally named perl -T -w. GNU Coreutils provides -S (or --split-string) to split the interpreter string into arguments:

#!/usr/bin/env -S perl -T -w

GNU -S is not universal across Unix-like systems or older implementations. Test the target platforms when distributing such a script. To inspect GNU’s parsing, run env -vS 'perl -T -w' script.pl; GNU documents -S and its parsing behavior in the env invocation manual.

Run shell builtins and shell syntax

env starts an executable; it does not directly interpret pipelines, redirections, aliases, functions, or shell builtins. For example, env cd /tmp does not change your current directory, and env export NAME=value does not export a variable in your shell. POSIX cautions against using a special built-in utility as the command operand. Read the POSIX specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start a shell when you need shell syntax:

env NAME=value sh -c 'printf "%sn" "$NAME"'
env PATH="$PATH" bash -c 'cd /tmp && pwd'

Any cd or export inside that shell affects only the child shell. Its state disappears when it exits.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Know which options are portable

The basic POSIX interface includes environment printing, NAME=VALUE assignments, command invocation, and -i. The following features are GNU Coreutils extensions and should not be assumed on every Unix-like system:

Feature POSIX GNU Coreutils
Print environment Yes Yes
NAME=VALUE and command invocation Yes Yes
-i (ignore inherited environment) Yes Yes
-u (unset named variable) No Yes
-C (change working directory) No Yes
-S (split interpreter string) No Yes
-0 (NUL-delimited output) No Yes
-v (show execution details) No Yes
Signal-handling options No Yes

The GNU manual currently documents Coreutils 9.11; installed systems can differ. Check env --version on a GNU system, or consult that system’s manual page. GNU Coreutils manual.

Other GNU options include -0 for NUL-terminated environment output, useful with tools that understand NUL delimiters; -v to show how the command is launched; --argv0=ARG to set the launched command’s argv[0]; and signal options such as --block-signal, --default-signal, and --ignore-signal. These are specialized controls, not portable baseline behavior. Details are in the GNU env manual page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot command failures

  • Command not found: A name without a slash is resolved through PATH. Supply a usable PATH after -i, or use an explicit path such as ./mycommand or an absolute pathname.
  • Command found but cannot run: Check that the file exists, has execute permission, and names a valid interpreter in its shebang. A missing shebang interpreter can also produce a “No such file or directory” error even when the script file exists.
  • Shell syntax is ignored: Pipes, redirects, cd, and variable expansion require a shell such as sh -c '...'; env itself does not parse them.
  • Variable has the wrong value: Check which shell expands it and whether it was quoted. Use env NAME="$VALUE" sh -c 'printf "%sn" "$NAME"' to pass a value to a child shell.
  • Shebang options are treated as one name: Avoid multiple arguments after /usr/bin/env unless the target implementation supports GNU -S; test on the systems where the script must run.
  • Need to inspect a value: Use printenv NAME for an environment variable. In Bash, set displays broader shell state, including variables and functions, while env displays the environment passed to a process.

Understand env exit statuses

GNU Coreutils env uses these statuses; the text of an error message can vary by implementation and locale, so scripts should check the numeric status instead of matching diagnostics. The GNU env manual page documents these cases.

Status Meaning
0 No command was supplied and the environment printed successfully.
125 env itself failed.
126 The command was found but could not be invoked.
127 The command could not be found.
Other status The launched command’s exit status.

For example, env definitely-not-a-command typically exits with status 127 under GNU Coreutils.

Keep environment variables in perspective

Environment variables are useful process configuration, not a dedicated secret vault. A value passed to a child is available to that process and may also appear in diagnostics, process inspection, crash reports, logs, CI interfaces, or debugging tools. Avoid putting real passwords or tokens directly in command lines, and do not assume that an environment variable is hidden merely because it is not printed in normal output. Where available, prefer the application’s secret-file support, a credential helper, an operating-system keyring, or a secret manager.

A file named .env is a separate convention used by applications and tooling; the env utility does not generally load such a file by itself. A program may parse it, or a shell tool may source it. Sourcing a file as shell code executes its contents, so only do that with trusted files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.