Understanding Environment and Shell Local Variables
In Bash scripting, variables come in two distinct types: environment variables and shell local variables. Understanding the difference between them is crucial for writing effective scripts and managing your shell environment.
What Are Environment and Shell Local Variables?
When you start a terminal session, Bash initializes with a collection of predefined variables that customize your system’s behavior. These fall into two categories:
Environment Variables
- Scope: Available to the current shell and all child processes
- Lifetime: Persist until the session ends
- Purpose: Pass configuration data to programs
- Also known as: Exported variables
Shell Local Variables
- Scope: Available only to the current shell
- Lifetime: Exist only while the shell is running
- Purpose: Store temporary data for the current session
- Not inherited: Child processes don’t see them
Key Differences at a Glance
| Feature | Environment Variables | Shell Local Variables |
|---|---|---|
| Visibility | Current shell + child processes | Current shell only |
| Creation | Using export | Direct assignment |
| Inheritance | Yes, by child processes | No |
| Examples | PATH, HOME, USER | BASH_VERSION, PPID |
Viewing Variables
Environment Variables Only
env# orprintenvAll Variables (Environment + Local)
setSearching for Specific Variables
env | grep PATHset | grep bashGetting a Single Variable’s Value
printenv HOME# orecho $HOMEPractical Examples
Example 1: The PATH Variable
The PATH variable is a perfect example of an environment variable:
echo $PATHThis variable tells the shell where to look for executable commands. It’s inherited by every program you run.
Example 2: User-Specific Information
echo "User: $USER"echo "Home: $HOME"echo "Shell: $SHELL"These are all environment variables set when you log in.
Example 3: Shell Local Variables
# These are local to your current shellecho $BASH_VERSIONecho $PPID # Parent process IDTry running env | grep BASH_VERSION - you won’t find it because it’s not exported.
Creating Variables
Creating Shell Local Variables
my_var="hello"abc=123These variables exist only in your current shell.
Creating Environment Variables
export MY_VAR="hello"# or in two steps:MY_VAR="hello"export MY_VARNow MY_VAR is available to any child processes you spawn.
Making Variables Persistent
User-Specific Variables
Add to your shell configuration files:
~/.bashrc (for interactive shells):
export MY_CUSTOM_VAR="value"~/.bash_profile (for login shells):
export MY_CUSTOM_VAR="value"System-Wide Variables
For all users, edit system files (requires root):
/etc/environment:
MY_SYSTEM_VAR="value"/etc/profile or /etc/bash.bashrc:
export MY_SYSTEM_VAR="value"Real-World Use Cases
1. Adding Directories to PATH
# Add your scripts directory to PATHexport PATH="$PATH:$HOME/scripts"2. Setting Default Editors
export EDITOR=vimexport VISUAL=vim3. Custom Application Settings
export DATABASE_URL="postgresql://localhost/mydb"export API_KEY="your-secret-key"Important Commands
View All Environment Variables
envprintenvView a Specific Variable
printenv HOMEecho $HOMEView All Variables (including local)
setClean Up set Output
set -o posix # Removes functions from outputset # Now shows only variablesRemove a Variable
unset MY_VAR # Removes from current shellexport -n MY_VAR # Removes from environment (keeps it local)Common Environment Variables
| Variable | Purpose |
|---|---|
PATH | Directories to search for commands |
HOME | User’s home directory |
USER | Current username |
SHELL | Default shell |
PWD | Current working directory |
LANG | Language/locale settings |
EDITOR | Default text editor |
TERM | Terminal type |
Best Practices
- Use UPPERCASE for environment variables by convention
- Export only what’s needed - don’t pollute the environment
- Document your variables - add comments in config files
- Check before setting - avoid overwriting existing variables
- Use descriptive names -
MYAPP_DB_HOSTnotDBH
Summary
- Environment variables = Global, inherited, persistent
- Shell local variables = Local, temporary, isolated
export= The key to making variables environment-wideenv/printenv= View environment variablesset= View everything- Configuration files = Make variables persistent across sessions
What’s Next?
Now that you understand variables, you’re ready to learn about getting user input - an essential skill for creating interactive scripts!