Linux / Bash
Beginner
1 min read
Environment Variables and the Shell Environment
Example
# List all environment variables
env
printenv
# Print a specific variable
printenv HOME
echo $HOME
echo $USER
echo $SHELL
# Assign a shell-local variable (NOT exported)
MY_VAR="hello"
bash -c 'echo $MY_VAR' # Empty — not exported
# Export a variable (available to child processes)
export MY_VAR="hello"
bash -c 'echo $MY_VAR' # Prints: hello
# Export and assign in one step
export API_URL="https://api.example.com"
# Check if a variable is set
if [[ -n "${MY_VAR:-}" ]]; then
echo "MY_VAR is set to: $MY_VAR"
fi
# Temporarily override a variable for one command
LANG=C ls --help
# Show common important variables
echo "Home: $HOME"
echo "User: $USER"
echo "Shell: $SHELL"
echo "Editor: ${EDITOR:-not set}"
echo "Term: $TERM"
echo "Path: $PATH"
# Unset a variable
unset MY_VAR
# Store variable in a script-friendly way using .env file
# cat .env:
# DB_HOST=localhost
# DB_PORT=5432
# Source (load) the .env file into current shell
# source .env OR . .env
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash