The lazy programmer scripts
scripts that work

Lazy create associative array-like construction in Bash GitHub

This scripts work on Bash.

Tested on RHEL 7, Ubuntu 14.04, 16.04

#!/bin/bash
set -e

ASSOC_ARRAY_KEYS=("KEY1" "KEY2")
KEY1_VAL="VAL1"
KEY2_VAL="VAL2"

for ASSOC_ARRAY_KEY in ${ASSOC_ARRAY_KEYS[@]}; do
    VAL_REF="${ASSOC_ARRAY_KEY}_VAL"
    echo "${!VAL_REF}"
    # It should print VAL1 and VAL2
done

That's all, folks!

Written by kami on Friday March 17, 2017
Permalink -

Leave a comment

comments powered by Disqus

« Lazy configure wi-fi hotspot on Windows - Lazy create Windows NAT adapter »