An associative array in Bash is a collection of key-value pairs where keys are strings instead of numbers. It lets you store and access data using named keys.
Click to reveal answer
beginner
How do you declare an associative array in Bash?
Use declare -A array_name to declare an associative array before using it.
Click to reveal answer
beginner
How do you assign a value to a key in an associative array?
Use array_name[key]=value. For example, colors[apple]=red assigns 'red' to the key 'apple'.
Click to reveal answer
beginner
How do you access a value from an associative array?
Use ${array_name[key]}. For example, ${colors[apple]} returns the value for the key 'apple'.
Click to reveal answer
intermediate
How can you list all keys in an associative array?
Use ${!array_name[@]} to get all keys. For example, ${!colors[@]} lists all keys in the 'colors' array.
Click to reveal answer
Which command declares an associative array named 'fruits' in Bash?
Adeclare -a fruits
Bdeclare fruits
Carray fruits
Ddeclare -A fruits
✗ Incorrect
Use declare -A to declare an associative array. declare -a is for indexed arrays.
How do you assign the value 'yellow' to the key 'banana' in an associative array named 'fruits'?
Afruits[banana]=yellow
Bfruits["banana"]="yellow"
Cfruits('banana')=yellow
Dfruits{banana}=yellow
✗ Incorrect
Keys and values with spaces or special characters should be quoted. The correct syntax is fruits["banana"]="yellow".
How do you print the value of the key 'apple' from the associative array 'fruits'?
Aecho ${fruits[apple]}
Becho fruits[apple]
Cecho ${fruits.apple}
Decho fruits(apple)
✗ Incorrect
Use ${array_name[key]} to access values. So echo ${fruits[apple]} prints the value.
Which expression lists all keys of an associative array named 'fruits'?
A${!fruits[@]}
B${fruits[@]}
C${fruits[*]}
D${#fruits[@]}
✗ Incorrect
${!fruits[@]} returns all keys. ${fruits[@]} returns all values.
What happens if you try to use an associative array without declaring it with declare -A?
AIt works normally
BBash treats it as an indexed array
CBash throws an error
DIt creates a string variable
✗ Incorrect
Bash throws an error (e.g., 'must use subscript when assigning to an array') because non-numeric keys require declare -A for associative arrays.
Explain how to create and use an associative array in Bash with an example.
Think about how you name the array, add keys and values, and get values back.
You got /4 concepts.
Describe how to list all keys and all values from an associative array in Bash.
Remember the special syntax with exclamation mark for keys.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using declare -A in a Bash script?
easy
A. To define a function
B. To declare a regular indexed array
C. To create an associative array with named keys
D. To start a loop
Solution
Step 1: Understand the role of declare -A
This command is used in Bash to declare an associative array, which means the array uses named keys instead of numbers.
Step 2: Differentiate from other declarations
Regular arrays use numbers as keys and are declared without -A. Functions and loops are declared differently.
Final Answer:
To create an associative array with named keys -> Option C
Quick Check:
declare -A means associative array [OK]
Hint: Remember: -A means associative array with named keys [OK]
Common Mistakes:
Confusing associative arrays with regular indexed arrays
Thinking declare -A creates a function
Assuming declare -A starts a loop
2. Which of the following is the correct syntax to declare an associative array named colors in Bash?
easy
A. declare colors=(red green blue)
B. declare -A colors
C. array colors=(red green blue)
D. declare -a colors
Solution
Step 1: Identify the correct declaration for associative arrays
In Bash, declare -A is used to declare associative arrays, so declare -A colors is correct.
Step 2: Understand other options
declare -a is for indexed arrays, and the other options are invalid syntax for associative arrays.
Final Answer:
declare -A colors -> Option B
Quick Check:
Associative array declaration uses declare -A [OK]
Hint: Use declare -A to declare associative arrays [OK]
Common Mistakes:
Using declare -a which is for indexed arrays
Trying to assign values during declaration incorrectly
Using invalid syntax like array keyword
3. What will be the output of the following Bash script?
declare -A fruits
fruits[apple]=red
fruits[banana]=yellow
for key in "${!fruits[@]}"; do
echo "$key is ${fruits[$key]}"
done
medium
A. apple banana\nred yellow
B. red is apple\nyellow is banana
C. Syntax error
D. apple is red\nbanana is yellow
Solution
Step 1: Understand the associative array and loop
The array fruits has keys 'apple' and 'banana' with values 'red' and 'yellow'. The loop iterates over keys and prints key and value pairs.
Step 2: Predict the output format
Each line prints "key is value", so output lines are "apple is red" and "banana is yellow" (order may vary but both lines appear).
Final Answer:
apple is red\nbanana is yellow -> Option D
Quick Check:
Loop prints key and value pairs correctly [OK]
Hint: Loop keys with ${!array[@]} to access associative array keys [OK]
Common Mistakes:
Printing values as keys or vice versa
Expecting syntax error due to quotes
Confusing indexed arrays with associative arrays
4. Identify the error in the following Bash script that uses an associative array:
declare -A animals
animals=([dog]=bark [cat]=meow)
echo ${animals[dog]}
medium
A. No error, script runs correctly
B. Associative array cannot be assigned values this way
C. Missing quotes around keys in assignment
D. Incorrect syntax for echo command
Solution
Step 1: Check associative array assignment syntax
Assigning values with animals=([dog]=bark [cat]=meow) is valid syntax for associative arrays in Bash.
Step 2: Verify echo command usage
echo ${animals[dog]} correctly prints the value for key 'dog'. No syntax errors present.
Final Answer:
No error, script runs correctly -> Option A
Quick Check:
Associative array assignment and access are correct [OK]
Hint: Assign associative arrays with ([key]=value ...) syntax [OK]
Common Mistakes:
Thinking quotes are mandatory around keys in assignment
Assuming echo syntax is wrong without quotes
Confusing indexed array assignment with associative arrays
5. You want to create an associative array user_ages that stores names as keys and ages as values. Which script correctly adds entries and prints all names with their ages?
hard
A. declare -A user_ages
user_ages=( [Alice]=30 [Bob]=25 )
for name in "${!user_ages[@]}"; do
echo "$name is ${user_ages[$name]} years old"
done
B. declare user_ages
user_ages=( Alice=30 Bob=25 )
for name in "${user_ages[@]}"; do
echo "$name is ${user_ages[$name]} years old"
done
C. declare -A user_ages
user_ages=( Alice:30 Bob:25 )
for name in "${!user_ages[@]}"; do
echo "$name is ${user_ages[$name]} years old"
done
D. declare -a user_ages
user_ages=( [Alice]=30 [Bob]=25 )
for name in "${!user_ages[@]}"; do
echo "$name is ${user_ages[$name]} years old"
done
Solution
Step 1: Check declaration and assignment
declare -A user_ages
user_ages=( [Alice]=30 [Bob]=25 )
for name in "${!user_ages[@]}"; do
echo "$name is ${user_ages[$name]} years old"
done correctly declares an associative array with declare -A and assigns keys and values using ( [key]=value ) syntax.
Step 2: Verify loop and output
The loop uses ${!user_ages[@]} to get keys and prints each name with its age correctly.
Final Answer:
Option A script correctly adds and prints associative array entries -> Option A
Quick Check:
Use declare -A and [key]=value syntax for associative arrays [OK]
Hint: Use declare -A and [key]=value syntax for associative arrays [OK]
Common Mistakes:
Using declare -a which is for indexed arrays
Incorrect key-value syntax like colons or missing brackets
Looping over values instead of keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.