The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Print exported valuesprintenvView examples
Read one variableprintenv PATHView examples
Set a shell variablemode=developmentView examples
Export to childrenexport MODE=developmentView examples
Scope to one commandMODE=development ./appView examples
Remove a variableunset MODEView examples
Prepend a PATH directoryexport PATH="$HOME/.local/bin:$PATH"View examples
Resolve a commandcommand -v python3View examples
Start cleanenv -i PATH=/usr/bin:/bin HOME="$HOME" commandView examples
Inspect localelocaleView examples
List localeslocale -aView examples
Inspect manager environmentsystemctl show-environmentView examples
Set a unit variableEnvironment=APP_MODE=productionView examples
Load an environment fileEnvironmentFile=/etc/myapp/environmentView examples

An environment is a per-process mapping inherited by child processes, not a global store. Distinguish shell variables from exported values, scope one-command overrides narrowly, edit the startup file that actually applies, and configure services through their manager rather than assuming an interactive shell profile runs.

Step by step

Detailed examples

01

Inspect the process you are actually debugging

Shell variables exist in the shell; only exported variables enter child environments. printenv and env show exported values, while the shell set builtin includes much more state. Secrets in environments may be exposed to same-user processes, diagnostics, or crash reports.

Compare shell and child visibility
local_only=alpha
export inherited=beta
printf 'shell: %s %s\n' "$local_only" "$inherited"
sh -c 'printf "child: local=%s inherited=%s\n" "${local_only-unset}" "$inherited"'
Back to quick reference ↑
02

Use the narrowest lifetime

An assignment preceding a simple command affects that command without permanently changing the shell. export affects future children, not existing processes or parent shells. unset only changes the current shell and descendants started afterward.

Temporary application mode
APP_MODE=testing ./run-tests
export APP_MODE=development
./start-dev
unset APP_MODE
Back to quick reference ↑
03

Treat PATH order as executable selection policy

PATH is searched left to right. Empty entries and dot can execute from the current directory, while writable directories before system paths enable shadowing. Quote the assignment, avoid duplicates, and confirm resolution.

Add and verify a user bin directory
case :$PATH: in
  *:$HOME/.local/bin:*) ;;
  *) export PATH="$HOME/.local/bin:$PATH" ;;
esac
command -v python3
type -a python3
Back to quick reference ↑
04

Make locale-sensitive behavior explicit

LANG supplies a default; LC_* values override categories; LC_ALL overrides all categories temporarily and should rarely be persisted. Locale affects sorting, character classes, messages, and formatting. TZ affects process time-zone interpretation.

Run deterministic formatting
locale
printf '%s\n' z a B | LC_ALL=C sort
TZ=UTC date '+%Y-%m-%dT%H:%M:%SZ'
Back to quick reference ↑
05

Configure services in the service manager

systemd system services do not normally source interactive shell profiles. Put non-secret values in unit drop-ins or EnvironmentFile, protect files, reload after unit changes, and restart only after verifying the merged unit. Use dedicated secret mechanisms for credentials.

Inspect a service environment contract
systemctl cat myapp.service
systemctl show myapp.service -p Environment -p EnvironmentFiles
systemd-analyze verify /etc/systemd/system/myapp.service
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Free Software FoundationBash Startup Filesgnu.org
  2. Free Software FoundationBash Variablesgnu.org
  3. Linux man-pages projectenviron(7)man7.org
  4. systemd Projectsystemd.execfreedesktop.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback