From d1146a45d6b92ffe78f8e9b4c8085c7bbbaeb938 Mon Sep 17 00:00:00 2001 From: haydn Date: Fri, 1 Jul 2022 22:37:32 +0930 Subject: [PATCH] Add 'bash/associative_array.bash' Associative Arrays and how to iterate over them --- bash/associative_array.bash | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 bash/associative_array.bash diff --git a/bash/associative_array.bash b/bash/associative_array.bash new file mode 100644 index 0000000..bf3eb03 --- /dev/null +++ b/bash/associative_array.bash @@ -0,0 +1,19 @@ +#!/bin/bash +# +# associative_array.bash. Derived from: +# https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash + +# Make an array and stuff it full of values +declare -A array +array[repo_one]=true +array[repo_two]=true +array[repo_three]=false + +# The key is accessed using the exclamation point +for i in "${!array[@]}" +do + echo "key : $i" + if ${array[$i]}; then + echo "$i is true" + fi +done \ No newline at end of file