0
0
Bash Scriptingscripting~10 mins

Associative arrays (declare -A) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Associative arrays (declare -A)
Start script
Declare associative array
Assign key-value pairs
Access or modify values by key
Iterate over keys or values
Use values as needed
End script
This flow shows how to declare an associative array, add key-value pairs, access or change values by keys, and iterate over them.
Execution Sample
Bash Scripting
declare -A fruits
fruits[apple]=red
fruits[banana]=yellow
echo ${fruits[apple]}
Declare an associative array 'fruits', assign colors to keys, then print the color of apple.
Execution Table
StepActionArray StateOutput
1declare -A fruitsfruits = {}
2fruits[apple]=redfruits = {apple: red}
3fruits[banana]=yellowfruits = {apple: red, banana: yellow}
4echo ${fruits[apple]}fruits = {apple: red, banana: yellow}red
5End of scriptfruits = {apple: red, banana: yellow}
💡 Script ends after printing the value for key 'apple'.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
fruitsundefinedapple: redapple: red, banana: yellowapple: red, banana: yellow
Key Moments - 3 Insights
Why do we use 'declare -A' before using an associative array?
In step 1, 'declare -A fruits' tells bash this array will use string keys. Without it, bash treats arrays as indexed with numbers.
Can we use numbers as keys in associative arrays?
Yes, keys can be any string, including numbers as strings. But you must still declare the array with '-A' to use string keys (see steps 2 and 3).
What happens if we try to access a key that doesn't exist?
If you access a missing key like ${fruits[orange]}, bash returns an empty string. This is not shown in the table but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 4?
Ared
Byellow
Capple
Dbanana
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the array first contain the key 'banana'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Array State' column to see when 'banana' appears.
If we add 'fruits[orange]=orange' after step 3, what will be the final array state?
A{apple: red, banana: yellow}
B{apple: red, banana: yellow, orange: orange}
C{orange: orange}
D{}
💡 Hint
Adding a new key-value pair appends it to the existing array state.
Concept Snapshot
declare -A arrayName  # Declare associative array
arrayName[key]=value     # Assign value to key
${arrayName[key]}        # Access value by key
Keys are strings, not numbers
Use loops to iterate keys or values
Full Transcript
This example shows how to declare an associative array in bash using 'declare -A'. We assign string keys like 'apple' and 'banana' with values 'red' and 'yellow'. Accessing a value uses the syntax ${array[key]}. The execution table traces each step: declaration, assignments, and output. Variables track how the array grows. Key moments clarify why declaration is needed, key types, and missing keys. The quiz tests understanding of output, array state changes, and adding new keys.