Skip to content

Scope

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_var.sh
#!/usr/bin/env bash
# Global variable - accessible everywhere
GLOBAL_VAR="I'm global"
show_global() {
echo "Inside function: $GLOBAL_VAR"
}
echo "Before function: $GLOBAL_VAR"
show_global
echo "After function: $GLOBAL_VAR"
local_var.sh
#!/usr/bin/env bash
GLOBAL="Global"
test_local() {
local LOCAL="I'm local"
echo "Inside: GLOBAL=$GLOBAL, LOCAL=$LOCAL"
}
test_local
# echo "$LOCAL" # Error: LOCAL is not defined here

modify_global.sh
#!/usr/bin/env bash
counter=0
increment() {
counter=$((counter + 1))
echo "Inside: counter=$counter"
}
echo "Before: counter=$counter"
increment
increment
increment
echo "After: counter=$counter"
pass_var.sh
#!/usr/bin/env bash
# Pass by value
double() {
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 value
echo "Value: $value"

export_var.sh
#!/usr/bin/env bash
# Export makes variable available to child processes
export MY_VAR="exported"
# Child process can see it
bash -c 'echo "Child sees: $MY_VAR"'
# Non-exported variable
NOT_EXPORTED="hidden"
bash -c 'echo "Child sees: $NOT_EXPORTED"' # Empty

config_scope.sh
#!/usr/bin/env bash
# Global config
CONFIG_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"
}
# Usage
load_config "database"
load_config "port"
private_vars.sh
#!/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
_helper
public_function

best_local.sh
#!/usr/bin/env bash
# WRONG: Using global variables in functions
process() {
result="processed" # Pollutes global scope
}
# CORRECT: Use local
process() {
local result="processed"
echo "$result"
}
minimize_global.sh
#!/usr/bin/env bash
# WRONG: Multiple globals
var1="value1"
var2="value2"
var3="value3"
# CORRECT: Group related variables
declare -A CONFIG
CONFIG[key1]="value1"
CONFIG[key2]="value2"
CONFIG[key3]="value3"

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

  1. Create a script with global and local variables
  2. Modify a global variable from a function
  1. Create a configuration system using scope
  2. Implement private/public function pattern
  1. Create a state management system
  2. Implement pass-by-reference

Continue to the next chapter to learn about command line arguments.


Previous Chapter: Functions Next Chapter: Command Line Arguments