Environment Basics
Bash · Reference cheat sheet
Environment Basics
Bash · Reference cheat sheet
📋 Overview
The environment is a set of key/value variables available to your shell and the programs it starts. Common ones: PATH, HOME, USER, PWD. Scripts often read VAR=value settings for configuration.
🔧 Core concepts
| Idea | Meaning |
|---|---|
| Environment variable | Name → string value |
export | Mark a variable for child processes |
PATH | Colon-separated list of directories to search for commands |
$VAR / "$\{VAR\}" | Expand a variable |
.env files | Convention for app config (not automatic in Bash) |
Shell variables and environment variables are related; export promotes a shell variable into the environment.
💡 Examples
Inspect common variables:
echo "$HOME"
echo "$USER"
echo "$PWD"
echo "$PATH"Set for one command:
LANG=C date
MESSAGE="hi" bash -c 'echo "$MESSAGE"'Export for the current session:
export APP_ENV=development
echo "$APP_ENV"
env | grep APP_ENVAdd a directory to PATH (session only):
export PATH="$HOME/bin:$PATH"
which python || true⚠️ Pitfalls
- Always quote expansions:
"$VAR"— unquoted values with spaces break. export FOO = baris wrong (spaces around=).- Changing
PATHincorrectly can make every command “not found.” - Putting secrets in world-readable scripts or committing
.envis unsafe.