The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Print exported values | printenv | View examples |
| Read one variable | printenv PATH | View examples |
| Set a shell variable | mode=development | View examples |
| Export to children | export MODE=development | View examples |
| Scope to one command | MODE=development ./app | View examples |
| Remove a variable | unset MODE | View examples |
| Prepend a PATH directory | export PATH="$HOME/.local/bin:$PATH" | View examples |
| Resolve a command | command -v python3 | View examples |
| Start clean | env -i PATH=/usr/bin:/bin HOME="$HOME" command | View examples |
| Inspect locale | locale | View examples |
| List locales | locale -a | View examples |
| Inspect manager environment | systemctl show-environment | View examples |
| Set a unit variable | Environment=APP_MODE=production | View examples |
| Load an environment file | EnvironmentFile=/etc/myapp/environment | View 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
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.
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"' 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.
APP_MODE=testing ./run-tests
export APP_MODE=development
./start-dev
unset APP_MODE 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.
case :$PATH: in
*:$HOME/.local/bin:*) ;;
*) export PATH="$HOME/.local/bin:$PATH" ;;
esac
command -v python3
type -a python3 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.
locale
printf '%s\n' z a B | LC_ALL=C sort
TZ=UTC date '+%Y-%m-%dT%H:%M:%SZ' 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.
systemctl cat myapp.service
systemctl show myapp.service -p Environment -p EnvironmentFiles
systemd-analyze verify /etc/systemd/system/myapp.service Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



