Global Variable Scope Issue in build_drive_map() #1

Open
opened 2026-02-02 14:54:43 -05:00 by jared · 0 comments
Owner

Line ~200-210

build_drive_map() {
local host=$(hostname)
declare -A drive_map # Local associative array

# ... population logic ...

# This makes it global, but it's inefficient and error-prone
declare -g -A DRIVE_MAP=()
for key in "${!drive_map[@]}"; do
    DRIVE_MAP[$key]=${drive_map[$key]}
done

}
Issue: Creating a local array then copying to global is inefficient. Should declare

DRIVE_MAP as global at the start.

Fix: Use

declare -g -A DRIVE_MAP at function start, populate directly.

# Line ~200-210 build_drive_map() { local host=$(hostname) declare -A drive_map # Local associative array # ... population logic ... # This makes it global, but it's inefficient and error-prone declare -g -A DRIVE_MAP=() for key in "${!drive_map[@]}"; do DRIVE_MAP[$key]=${drive_map[$key]} done } Issue: Creating a local array then copying to global is inefficient. Should declare DRIVE_MAP as global at the start. Fix: Use declare -g -A DRIVE_MAP at function start, populate directly.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/driveAtlas#1