Use this when scripts depend on environment variables.
Environment Variables · Bash Automation
Environment variables in Bash are exported variables passed to child processes, such as PATH or HOME. Set them with export NAME=value, and remember cron and systemd start with a minimal environment.
1. Environment variables are inherited by child processes.
2. cron/systemd have minimal environments — set PATH explicitly.
3. Don't leak secrets via env to logs.
4. Document required variables.
5. Use ${VAR:?} to require.
parent env → exported vars → child processes
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- Expecting your login shell's variables inside cron.
- Secrets exported in shell history.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- export passes variables to child processes.
- cron and systemd start with minimal environment.
- Set PATH explicitly in scripts.
printenv | sort— environmentenv -i bash --noprofile --norc -c 'printenv'— minimal env (cron-like)export VAR=value— export
ReferenceWhy it matters1 section · depth on demand
What it is
Environment variables pass settings to processes (PATH, HTTPS_PROXY, tokens). export makes a variable visible to child processes; cron and systemd start with a minimal environment.
ReferenceImportant terms & boundaries2 sections · depth on demand
How it works
A child process gets a copy of the parent's exported environment at start; changes in the child never flow back. Login shells read /etc/profile and ~/.profile, interactive shells ~/.bashrc; cron and systemd start with almost nothing, so set what a job needs explicitly.
Example
export API_URL=https://netbox.example.com
env | sort
# systemd: Environment=API_URL=... or EnvironmentFile=/etc/myjob.env
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Environment Variables. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%