0
0
Bash Scriptingscripting~10 mins

Associative arrays (declare -A) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an associative array named 'colors'.

Bash Scripting
declare [1] colors
Drag options to blanks, or click blank then click option'
A-A
B-a
C-r
D-i
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase -a which declares an indexed array instead.
Using options unrelated to arrays like -i or -r.
2fill in blank
medium

Complete the code to assign the value 'blue' to the key 'sky' in the associative array 'colors'.

Bash Scripting
colors[[1]]=blue
Drag options to blanks, or click blank then click option'
A'sky'
Bsky
C"sky"
Dsky_key
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the key which can cause syntax errors.
Using a different key name than intended.
3fill in blank
hard

Fix the error in the code to print the value of the key 'sky' from the associative array 'colors'.

Bash Scripting
echo ${colors[1]
Drag options to blanks, or click blank then click option'
A<sky>
B(sky)
C{sky}
D[sky]
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces or parentheses instead of square brackets.
Forgetting to quote the key if it contains spaces.
4fill in blank
hard

Fill both blanks to declare an associative array and assign 'red' to key 'apple'.

Bash Scripting
declare [1] fruits; fruits[[2]]=red
Drag options to blanks, or click blank then click option'
A-A
B"apple"
C'apple'
D-a
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase -a which declares indexed arrays.
Not quoting the key string.
5fill in blank
hard

Fill all three blanks to create an associative array 'person', assign 'John' to 'name', and print it.

Bash Scripting
declare [1] person; person[[2]]=John; echo ${person[3]
Drag options to blanks, or click blank then click option'
A-A
B"name"
C["name"]
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong declaration option.
Not quoting keys consistently.
Using wrong brackets to access array elements.