0
0
Bash Scriptingscripting~5 mins

Indexed array declaration in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an indexed array in Bash scripting?
An indexed array in Bash is a list of values stored in a single variable, where each value has a numeric index starting from 0.
Click to reveal answer
beginner
How do you declare an indexed array with values in Bash?
Use syntax: array_name=(value1 value2 value3). For example: fruits=(apple banana cherry).
Click to reveal answer
beginner
How do you access the second element of an indexed array named colors?
Use ${colors[1]} because indexing starts at 0.
Click to reveal answer
intermediate
How to add a new element to an existing indexed array in Bash?
Use syntax: array_name+=(new_value). For example: fruits+=('orange') adds 'orange' to the fruits array.
Click to reveal answer
beginner
What command shows all elements of an indexed array named my_array?
Use echo ${my_array[@]} to print all elements separated by spaces.
Click to reveal answer
How do you declare an indexed array named pets with elements 'dog', 'cat', and 'bird'?
Apets<dog cat bird>
Bpets=[dog, cat, bird]
Cpets=(dog cat bird)
Dpets={dog cat bird}
What is the index of the first element in a Bash indexed array?
A1
B-1
CDepends on the array
D0
How do you print all elements of an indexed array colors?
Aecho ${colors[@]}
Becho ${colors[*]}
Cecho colors
Decho colors[]
How to add 'pear' to an existing array fruits?
Afruits.append('pear')
Bfruits+=('pear')
Cfruits.push('pear')
Dfruits = fruits + 'pear'
Which syntax is correct to access the third element of array numbers?
A${numbers[2]}
B${numbers[3]}
C${numbers(2)}
D${numbers{2}}
Explain how to declare and access elements in an indexed array in Bash.
Think about how you list items in a shopping list and then pick one by its position.
You got /4 concepts.
    Describe how to add a new element to an existing indexed array in Bash.
    Imagine adding a new item to your list without rewriting the whole list.
    You got /4 concepts.