📜 BASH SCRIPT DIFFICULTY: Adept Creational

The Prototype Shell Cloner

Duplicating stateful shell configurations via the Prototype pattern.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Prototype
ARCANUM SCHOOL: Illusion // Cloning
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE BASH SCRIPT //
#!/usr/bin/env bash

# Prototype base config
declare -A PROTOTYPE_CONFIG=(
  [timeout]="30"
  [retries]="3"
  [verbosity]="low"
)

# Clone method
clone_config() {
  local new_config_name=$1
  # We use declare -n (nameref) to create a copy in a new array
  declare -gA "$new_config_name"

  for key in "${!PROTOTYPE_CONFIG[@]}"; do
    eval "$new_config_name[$key]=\"\${PROTOTYPE_CONFIG[$key]}\""
  done
}

# Usage
clone_config "HACK_CONFIG"
HACK_CONFIG[timeout]="10"
HACK_CONFIG[verbosity]="high"

echo "Prototype Verbosity: ${PROTOTYPE_CONFIG[verbosity]}"
echo "Cloned Hack Verbosity: ${HACK_CONFIG[verbosity]}"

Why rebuild a complex array of environmental variables when you can simply clone it? The Prototype pattern in Bash leverages the dark art of eval and associative arrays to stamp out perfect duplicates of configurations, allowing immediate mutation without altering the original blueprint.

Runic Tags: #bash #prototype #creational #cloning