CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / env

env

Show the environment, or run a command with a changed one

Beginneressentialenvironmentshell

env prints the environment variables the current process will pass to the programs it starts, or runs one command with variables added, removed or cleared. It's also the /usr/bin/env in '#!/usr/bin/env python3' shebangs.

Syntaxenv [-i] [-u NAME]... [NAME=VALUE]... [COMMAND [ARG]...]

Watch it run

You've just SSHed into a server and want to see which variables your session will pass to programs you start.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
env | sort

Every variable here is copied into each program you run. Sorting makes it readable; the raw order is whatever order the shell built it in.

Anatomy of a command

$
commandenv

/usr/bin/env from coreutils. Without a command it prints its environment, one NAME=value per line.

How it works

Every process has an environment: an array of NAME=value strings that the kernel copies into the new process image when a program calls execve(path, argv, envp). It sits at the top of the process's stack and is visible later in /proc/<pid>/environ. env is a tiny program: with no command it walks its environ array and prints it, which shows exactly what your shell exported. With assignments, -u or -i it builds a modified array, then calls execvp so the command replaces env in the same process with that environment. execvp searches PATH, which is why '#!/usr/bin/env python3' finds python3 wherever it is installed. Changing a variable later in a running process (setenv) doesn't update /proc/<pid>/environ, and nothing a child does to its environment ever flows back to its parent. Shell variables that aren't exported aren't in the environment at all, so env doesn't show them.

Options you'll actually use

(no args)Print the environment, one NAME=value per line
NAME=VALUE cmdRun cmd with NAME set or overridden
-i, --ignore-environmentStart with an empty environment
-u NAMERemove NAME from the command's environment
-0End each line with NUL instead of newline (safe parsing)
-S 'STRING'Split STRING into arguments (for shebangs with options)
-C DIRChange directory before running the command
printenv NAMEPrint one variable's value (exit 1 if unset)

Common mistakes

DB_HOST=x; ./app and expecting the app to see DB_HOST

A plain assignment is a shell variable. It isn't copied to child processes unless exported.

export DB_HOST=x; ./app # or for one run: DB_HOST=x ./app
Passing secrets as environment variables and assuming they're private

They're readable in /proc/PID/environ by root and the same user, and they leak into crash dumps, debug pages and child processes.

Read secrets from a 0600 file or a secrets manager at startup
env -i ./app to test a 'clean' run

Without PATH and HOME, the app (or the shell inside it) can't find commands and fails in confusing ways.

env -i HOME=$HOME PATH=/usr/bin:/bin ./app
Parsing env output to read values that contain newlines

A value with a newline looks like two variables.

env -0 | tr '\0' '\n' # or printenv NAME

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print all environment variables sorted by name and count them. (The sandbox shell is zsh, but env works the same.)

  2. 2

    Run 'bash -c env' with an empty environment plus only PATH=/usr/bin:/bin and see what's left.

  3. 3

    Show the environment your shell process started with by reading /proc.

  4. 4

    Run the report script ~/app/worker/report.py with a different data file path without editing it. Its first argument defaults to ~/data/orders.csv; check how its shebang finds python.

Related commands

exportPass variables to the programs you startsetShell options: set -euo pipefail, set -x and friendsunsetRemove variables and functions from the shellsourceRun a file's commands in the current shell
PreviousgetfaclNextexport
Linux / Environment & Shell / env

env

Show the environment, or run a command with a changed one

Beginneressentialenvironmentshell

env prints the environment variables the current process will pass to the programs it starts, or runs one command with variables added, removed or cleared. It's also the /usr/bin/env in '#!/usr/bin/env python3' shebangs.

Syntaxenv [-i] [-u NAME]... [NAME=VALUE]... [COMMAND [ARG]...]

Watch it run

You've just SSHed into a server and want to see which variables your session will pass to programs you start.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
env | sort

Every variable here is copied into each program you run. Sorting makes it readable; the raw order is whatever order the shell built it in.

Anatomy of a command

$
commandenv

/usr/bin/env from coreutils. Without a command it prints its environment, one NAME=value per line.

How it works

Every process has an environment: an array of NAME=value strings that the kernel copies into the new process image when a program calls execve(path, argv, envp). It sits at the top of the process's stack and is visible later in /proc/<pid>/environ. env is a tiny program: with no command it walks its environ array and prints it, which shows exactly what your shell exported. With assignments, -u or -i it builds a modified array, then calls execvp so the command replaces env in the same process with that environment. execvp searches PATH, which is why '#!/usr/bin/env python3' finds python3 wherever it is installed. Changing a variable later in a running process (setenv) doesn't update /proc/<pid>/environ, and nothing a child does to its environment ever flows back to its parent. Shell variables that aren't exported aren't in the environment at all, so env doesn't show them.

Options you'll actually use

(no args)Print the environment, one NAME=value per line
NAME=VALUE cmdRun cmd with NAME set or overridden
-i, --ignore-environmentStart with an empty environment
-u NAMERemove NAME from the command's environment
-0End each line with NUL instead of newline (safe parsing)
-S 'STRING'Split STRING into arguments (for shebangs with options)
-C DIRChange directory before running the command
printenv NAMEPrint one variable's value (exit 1 if unset)

Common mistakes

DB_HOST=x; ./app and expecting the app to see DB_HOST

A plain assignment is a shell variable. It isn't copied to child processes unless exported.

export DB_HOST=x; ./app # or for one run: DB_HOST=x ./app
Passing secrets as environment variables and assuming they're private

They're readable in /proc/PID/environ by root and the same user, and they leak into crash dumps, debug pages and child processes.

Read secrets from a 0600 file or a secrets manager at startup
env -i ./app to test a 'clean' run

Without PATH and HOME, the app (or the shell inside it) can't find commands and fails in confusing ways.

env -i HOME=$HOME PATH=/usr/bin:/bin ./app
Parsing env output to read values that contain newlines

A value with a newline looks like two variables.

env -0 | tr '\0' '\n' # or printenv NAME

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print all environment variables sorted by name and count them. (The sandbox shell is zsh, but env works the same.)

  2. 2

    Run 'bash -c env' with an empty environment plus only PATH=/usr/bin:/bin and see what's left.

  3. 3

    Show the environment your shell process started with by reading /proc.

  4. 4

    Run the report script ~/app/worker/report.py with a different data file path without editing it. Its first argument defaults to ~/data/orders.csv; check how its shebang finds python.

Related commands

exportPass variables to the programs you startsetShell options: set -euo pipefail, set -x and friendsunsetRemove variables and functions from the shellsourceRun a file's commands in the current shell
PreviousgetfaclNextexport