Web Analytics

Environment Variables

Beginner ~15 min read

Environment variables are special variables that are available to all processes started from your shell. They're inherited by child processes and are essential for system configuration and inter-process communication. Understanding environment variables is crucial for system administration, configuring tools, and writing scripts that work across different systems.

What are Environment Variables?

Environment variables are variables that are exported from your current shell to all child processes. They're different from regular variables because they're inherited by any program or script you run.

Output
Click Run to execute your code
Key Difference:
- Regular Variable: Only available in current shell
- Environment Variable: Available to current shell AND all child processes
Use export to make a variable an environment variable!

Common Environment Variables

Every Unix-like system has several standard environment variables that provide information about the system and user environment.

Variable Description Example Value
HOME User's home directory /home/username or /Users/username
USER / USERNAME Current username alice
PATH Colon-separated list of directories to search for executables /usr/bin:/bin:/usr/local/bin
SHELL Path to current shell /bin/bash
PWD Current working directory /home/user/projects
LANG / LC_ALL Language/locale settings en_US.UTF-8
EDITOR Default text editor vim or nano

The PATH Variable

The PATH variable is one of the most important environment variables. It tells the system which directories to search when you type a command. Without PATH, you'd have to type the full path to every command!

# View PATH
echo $PATH
# Output: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

# PATH is colon-separated
# When you type 'ls', system searches:
# 1. /usr/local/bin/ls
# 2. /usr/bin/ls
# 3. /bin/ls
# 4. (stops at first match)

# Add directory to PATH
export PATH="$PATH:/new/directory"
# Now system will also search /new/directory
Important: Always append to PATH with PATH="$PATH:/new/path" rather than replacing it! Replacing PATH (PATH="/new/path") will break most commands because standard directories like /bin and /usr/bin won't be searched anymore.

The export Command

To make a variable available to child processes (i.e., make it an environment variable), use the export command. This is essential when you want scripts or commands you run to access your variables.

# Regular variable (not exported)
MY_VAR="hello"
bash -c 'echo $MY_VAR'  # Output: (empty - child process can't see it)

# Export the variable
export MY_VAR="hello"
bash -c 'echo $MY_VAR'  # Output: hello (child process can see it)

# Alternative syntax
MY_VAR="hello"
export MY_VAR

# Combined declaration and export
export API_KEY="secret123"
export DEBUG="true"
When to Use export:
- When running scripts that need the variable
- When starting programs that check environment variables
- For configuration values (like API keys, paths)
- For flags (like DEBUG, VERBOSE)
Regular variables are fine for use within a single script.

Viewing Environment Variables

There are several commands to view environment variables, each with slightly different output formats.

Command Description Example
env List all environment variables env
printenv List all environment variables (same as env) printenv
printenv VAR Print specific variable value printenv HOME
set List all variables (including non-exported) set | grep MY_VAR
echo $VAR Print specific variable echo $HOME

Setting Environment Variables

You can set environment variables in several ways, depending on how long you want them to persist.

# 1. For current session only (temporary)
export MY_VAR="value"

# 2. For a single command
MY_VAR="value" command_to_run

# 3. In a script (affects script and its children)
#!/bin/bash
export CONFIG_FILE="/path/to/config"
python script.py  # python can see CONFIG_FILE

# 4. Permanently in ~/.bashrc or ~/.bash_profile
# Add to file: export MY_VAR="value"
# Then: source ~/.bashrc
Persistence:
- Temporary: export VAR="value" in terminal (lost when terminal closes)
- For User: Add to ~/.bashrc (Linux) or ~/.bash_profile (macOS)
- System-wide: Add to /etc/environment or /etc/profile (requires root)

Common Mistakes

1. Not exporting variables needed by child processes

# Wrong - child process can't see it
CONFIG_PATH="/etc/config"
python script.py  # script.py can't access CONFIG_PATH

# Correct - export it
export CONFIG_PATH="/etc/config"
python script.py  # script.py can access CONFIG_PATH

2. Replacing PATH instead of appending

# Wrong - breaks system commands
export PATH="/new/path"  # Now ls, cd, etc. won't work!

# Correct - append to existing PATH
export PATH="$PATH:/new/path"  # Adds to existing PATH

3. Setting environment variables without export

# Wrong - not exported, child can't see it
API_KEY="secret123"
bash script.sh  # script.sh can't access API_KEY

# Correct - export it
export API_KEY="secret123"
bash script.sh  # script.sh can access API_KEY

Exercise: Work with Environment Variables

Task: Create a script that demonstrates environment variable usage!

Requirements:

  • Display common environment variables (HOME, USER, PATH)
  • Create and export a custom environment variable
  • Show the difference between exported and non-exported variables
  • Count how many environment variables are set
  • Display the PATH variable, showing first 3 directories
Show Solution
#!/bin/bash
# Environment variables exercise

echo "Common environment variables:"
echo "  HOME: $HOME"
echo "  USER: $USER"
echo "  SHELL: $SHELL"
echo ""

# Create and export variable
export MY_CUSTOM_VAR="Hello from environment"
echo "Exported variable:"
echo "  MY_CUSTOM_VAR: $MY_CUSTOM_VAR"
echo ""

# Count environment variables
env_count=$(env | wc -l)
echo "Total environment variables: $env_count"
echo ""

# Display PATH
echo "PATH variable (first 3 directories):"
echo "$PATH" | tr ':' '\n' | head -3 | nl

Summary

  • Environment Variables: Available to current shell and all child processes
  • export: Use export VAR="value" to make variables available to child processes
  • PATH: Colon-separated directories where system searches for commands
  • Common Vars: HOME, USER, SHELL, PWD, PATH, LANG
  • Viewing: Use env, printenv, or echo $VAR
  • PATH Safety: Always append with PATH="$PATH:/new/path" never replace
  • Persistence: Add to ~/.bashrc or ~/.bash_profile for permanent settings

What's Next?

Perfect! You now understand environment variables. Next, we'll explore Special Variables - variables that Bash automatically sets, like $0 (script name), $1-$9 (arguments), $# (argument count), $@ (all arguments), and more. These are essential for writing scripts that accept command-line arguments!