Scope
Chapter 16: Scope and Variable Visibility
Section titled “Chapter 16: Scope and Variable Visibility”Overview
Section titled “Overview”Understanding variable scope is crucial for writing maintainable bash scripts. This chapter covers global variables, local variables, function scope, environment variables, and best practices for managing variable visibility in your scripts.
Global vs Local Variables
Section titled “Global vs Local Variables”Global Variables
Section titled “Global Variables”#!/usr/bin/env bash# Global variable - accessible everywhereGLOBAL_VAR="I'm global"
show_global() { echo "Inside function: $GLOBAL_VAR"}
echo "Before function: $GLOBAL_VAR"show_globalecho "After function: $GLOBAL_VAR"Local Variables
Section titled “Local Variables”#!/usr/bin/env bashGLOBAL="Global"
test_local() { local LOCAL="I'm local" echo "Inside: GLOBAL=$GLOBAL, LOCAL=$LOCAL"}
test_local# echo "$LOCAL" # Error: LOCAL is not defined hereFunction Scope
Section titled “Function Scope”Modifying Global Variables
Section titled “Modifying Global Variables”#!/usr/bin/env bashcounter=0
increment() { counter=$((counter + 1)) echo "Inside: counter=$counter"}
echo "Before: counter=$counter"incrementincrementincrementecho "After: counter=$counter"Passing Variables
Section titled “Passing Variables”#!/usr/bin/env bash# Pass by valuedouble() { local num=$1 echo $((num * 2))}
result=$(double 5)echo "Result: $result"
# Pass by reference (bash 4.3+)modify() { local -n ref="$1" ref="modified"}
value="original"modify valueecho "Value: $value"Environment Scope
Section titled “Environment Scope”Exporting Variables
Section titled “Exporting Variables”#!/usr/bin/env bash# Export makes variable available to child processesexport MY_VAR="exported"
# Child process can see itbash -c 'echo "Child sees: $MY_VAR"'
# Non-exported variableNOT_EXPORTED="hidden"bash -c 'echo "Child sees: $NOT_EXPORTED"' # EmptyPractical Examples
Section titled “Practical Examples”Example 1: Configuration Management
Section titled “Example 1: Configuration Management”#!/usr/bin/env bash# Global configCONFIG_FILE="/etc/app/config"
load_config() { # Local variables for function local key="$1" local default="${2:-}"
# Use global config echo "Loading $key from $CONFIG_FILE (default: $default)"}
get_config() { local key="$1" local default="${2:-default_value}"
# Return config value echo "$default"}
# Usageload_config "database"load_config "port"Example 2: Private Variables in Scripts
Section titled “Example 2: Private Variables in Scripts”#!/usr/bin/env bash# Script-private helper function_helper() { local secret="This is private" echo "Helper: $secret"}
public_function() { local public_var="This is public" echo "Public: $public_var"}
# Call functions_helperpublic_functionBest Practices
Section titled “Best Practices”Always Use local
Section titled “Always Use local”#!/usr/bin/env bash# WRONG: Using global variables in functionsprocess() { result="processed" # Pollutes global scope}
# CORRECT: Use localprocess() { local result="processed" echo "$result"}Minimize Global State
Section titled “Minimize Global State”#!/usr/bin/env bash# WRONG: Multiple globalsvar1="value1"var2="value2"var3="value3"
# CORRECT: Group related variablesdeclare -A CONFIGCONFIG[key1]="value1"CONFIG[key2]="value2"CONFIG[key3]="value3"Summary
Section titled “Summary”In this chapter, you learned about:
- ✅ Global variables
- ✅ Local variables
- ✅ Function scope
- ✅ Modifying global variables
- ✅ Passing variables (by value and reference)
- ✅ Environment scope
- ✅ Exporting variables
- ✅ Best practices
Exercises
Section titled “Exercises”Level 1: Basics
Section titled “Level 1: Basics”- Create a script with global and local variables
- Modify a global variable from a function
Level 2: Intermediate
Section titled “Level 2: Intermediate”- Create a configuration system using scope
- Implement private/public function pattern
Level 3: Advanced
Section titled “Level 3: Advanced”- Create a state management system
- Implement pass-by-reference
Next Steps
Section titled “Next Steps”Continue to the next chapter to learn about command line arguments.
Previous Chapter: Functions Next Chapter: Command Line Arguments