Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Indexed array declaration in Bash Scripting - Interactive Code Practice

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

Complete the code to declare an indexed array named 'fruits' with three elements.

Bash Scripting
fruits=[1]
Drag options to blanks, or click blank then click option'
A(apple banana cherry)
B[0]=apple [1]=banana [2]=cherry
C"apple" "banana" "cherry"
Dapple, banana, cherry
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas between elements instead of spaces.
Using quotes around the whole list instead of individual elements.
Using key=value pairs which is for associative arrays.
2fill in blank
medium

Complete the code to print the second element of the 'colors' array.

Bash Scripting
echo ${colors[1]
Drag options to blanks, or click blank then click option'
A[2]
B[0]
C[1]
D[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 instead of 1 for the second element.
Forgetting the square brackets around the index.
3fill in blank
hard

Fix the error in the array declaration to correctly declare an indexed array named 'numbers'.

Bash Scripting
numbers=[1]
Drag options to blanks, or click blank then click option'
A(1 2 3 4 5)
B[1 2 3 4 5]
C{1,2,3,4,5}
D"1 2 3 4 5"
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets which is invalid for array declaration.
Using curly braces or quotes which create strings, not arrays.
4fill in blank
hard

Fill both blanks to declare an indexed array 'pets' with three elements and print the first element.

Bash Scripting
pets=[1]
echo ${pets[2]
Drag options to blanks, or click blank then click option'
A(dog cat bird)
B[0]
C[1]
D"dog cat bird"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes instead of parentheses for declaration.
Using index 1 instead of 0 to access the first element.
5fill in blank
hard

Fill all three blanks to declare an indexed array 'tools', add a new element, and print the last element.

Bash Scripting
tools=[1]
tools[[2]]=[3]
echo ${tools[2]}
Drag options to blanks, or click blank then click option'
A(hammer screwdriver)
B2
C"wrench"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices when adding or printing elements.
Not using quotes around the new element string.

Practice

(1/5)
1. What is the correct way to declare an indexed array named fruits with values apple, banana, and cherry in Bash?
easy
A. fruits=[apple, banana, cherry]
B. fruits=("apple, banana, cherry")
C. fruits={apple banana cherry}
D. fruits=(apple banana cherry)

Solution

  1. Step 1: Understand Bash array declaration syntax

    Bash arrays use parentheses with space-separated values, no commas or braces.
  2. Step 2: Check each option's syntax

    fruits=(apple banana cherry) uses parentheses and spaces correctly. Square brackets and braces are incorrect. fruits=("apple, banana, cherry") treats all values as one string.
  3. Final Answer:

    fruits=(apple banana cherry) -> Option D
  4. Quick Check:

    Parentheses + spaces = correct array declaration [OK]
Hint: Use parentheses and spaces, no commas or braces [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Including commas between values
  • Putting all values inside quotes as one string
2. Which of the following is the correct syntax to declare an empty indexed array named numbers in Bash?
easy
A. numbers={}
B. numbers=()
C. numbers=[]
D. numbers=""

Solution

  1. Step 1: Recall empty array declaration in Bash

    An empty indexed array is declared with empty parentheses.
  2. Step 2: Evaluate each option

    numbers=() uses empty parentheses correctly. Braces and square brackets are invalid. numbers="" assigns an empty string, not an array.
  3. Final Answer:

    numbers=() -> Option B
  4. Quick Check:

    Empty parentheses declare empty array [OK]
Hint: Empty parentheses mean empty array [OK]
Common Mistakes:
  • Using square brackets or braces instead of parentheses
  • Assigning empty string instead of array
  • Confusing array declaration with variable assignment
3. Given the array declaration colors=(red green blue), what will be the output of echo ${colors[1]}?
medium
A. green
B. colors[1]
C. blue
D. red

Solution

  1. Step 1: Understand array indexing in Bash

    Indexed arrays start at 0, so index 1 is the second element.
  2. Step 2: Identify the element at index 1

    colors[0] = red, colors[1] = green, colors[2] = blue.
  3. Final Answer:

    green -> Option A
  4. Quick Check:

    Index 1 = second element = green [OK]
Hint: Array indexes start at zero [OK]
Common Mistakes:
  • Thinking array starts at 1 instead of 0
  • Printing the variable name instead of value
  • Confusing index with value
4. What is wrong with this array declaration?
myarray=(one two three four five six seven eight nine ten
medium
A. Array name is invalid
B. Values should be quoted
C. Missing closing parenthesis
D. Values must be comma separated

Solution

  1. Step 1: Check array syntax completeness

    The declaration starts with '(' but does not have a closing ')'.
  2. Step 2: Verify other syntax rules

    Values do not need quotes if no spaces, array name is valid, commas are not used in Bash arrays.
  3. Final Answer:

    Missing closing parenthesis -> Option C
  4. Quick Check:

    Parentheses must open and close [OK]
Hint: Always close parentheses in array declaration [OK]
Common Mistakes:
  • Forgetting the closing parenthesis
  • Adding commas between values
  • Quoting values unnecessarily
5. How can you declare an indexed array days with the values Mon, Tue, and Wed, then print the total number of elements in the array?
hard
A. days=(Mon Tue Wed); echo ${#days[@]}
B. days=[Mon Tue Wed]; echo ${#days}
C. days=(Mon, Tue, Wed); echo ${#days[@]}
D. days=(Mon Tue Wed); echo ${days.length}

Solution

  1. Step 1: Declare the array correctly

    Use parentheses with space-separated values: days=(Mon Tue Wed).
  2. Step 2: Use correct syntax to get array length

    Use ${#days[@]} to get the number of elements in the array.
  3. Final Answer:

    days=(Mon Tue Wed); echo ${#days[@]} -> Option A
  4. Quick Check:

    Parentheses + ${#array[@]} = count [OK]
Hint: Use ${#array[@]} to get array length [OK]
Common Mistakes:
  • Using square brackets for array declaration
  • Using commas between values
  • Using incorrect syntax for length like ${#days} or .length
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard