Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why arrays handle lists of data in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Mastery in Bash
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash array script?
Consider the following Bash script that uses arrays. What will it print?
Bash Scripting
arr=(apple banana cherry)
echo ${arr[1]}
Abanana
BSyntaxError
Ccherry
Dapple
Attempts:
2 left
💡 Hint
Remember that Bash arrays start counting from zero.
🧠 Conceptual
intermediate
2:00remaining
Why use arrays instead of separate variables in Bash?
Which reason best explains why arrays are useful for handling lists of data in Bash?
AArrays let you store multiple related values under one name with indexes.
BArrays automatically sort data alphabetically.
CArrays prevent any data from being changed once set.
DArrays allow you to run commands faster than variables.
Attempts:
2 left
💡 Hint
Think about how you would keep track of many items easily.
🔧 Debug
advanced
2:00remaining
Identify the error in this Bash array usage
What error will this script produce? arr=(one two three) echo ${arr[3]}
APrints 'three' because arrays wrap around
BSyntaxError due to invalid index
CPrints an empty line because index 3 does not exist
DPrints 'one' because it loops back to start
Attempts:
2 left
💡 Hint
Check the array length and valid indexes.
🚀 Application
advanced
2:00remaining
How to loop over all elements in a Bash array?
Which script correctly prints each element of the array 'fruits' on its own line?
Bash Scripting
fruits=(apple banana cherry)
Afor fruit in ${fruits}; do echo $fruit; done
Bfor fruit in "${fruits[@]}"; do echo $fruit; done
Cfor fruit in ${fruits[*]}; do echo $fruit; done
Dfor fruit in fruits; do echo $fruit; done
Attempts:
2 left
💡 Hint
Use the syntax that preserves each array element as a separate word.
📝 Syntax
expert
2:00remaining
Which option correctly declares and prints a Bash array element?
Select the option that correctly creates an array with elements 'red', 'green', 'blue' and prints the second element.
A
colors={red green blue}
echo ${colors[2]}
B
colors=[red, green, blue]
echo ${colors[1]}
C
colors=(red, green, blue)
echo ${colors[1]}
D
colors=(red green blue)
echo ${colors[1]}
Attempts:
2 left
💡 Hint
Remember the correct syntax for Bash arrays uses parentheses without commas.

Practice

(1/5)
1. Why do we use arrays in bash scripting?
easy
A. To create new files automatically
B. To store only one number at a time
C. To run commands faster
D. To store multiple related items in one variable

Solution

  1. Step 1: Understand what arrays do

    Arrays hold many items together in one variable, like a list.
  2. Step 2: Compare options

    Only To store multiple related items in one variable correctly says arrays store multiple related items.
  3. Final Answer:

    To store multiple related items in one variable -> Option D
  4. Quick Check:

    Arrays = store many items [OK]
Hint: Arrays hold lists, not single values [OK]
Common Mistakes:
  • Thinking arrays store only one value
  • Confusing arrays with commands
  • Believing arrays create files
2. Which is the correct way to declare an array named fruits with items 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: Recall bash array syntax

    Bash arrays use parentheses with space-separated items: array=(item1 item2).
  2. Step 2: Check each option

    Only fruits=(apple banana cherry) uses parentheses and spaces correctly.
  3. Final Answer:

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

    Array syntax = parentheses with spaces [OK]
Hint: Use parentheses and spaces for arrays [OK]
Common Mistakes:
  • Using square brackets like other languages
  • Separating items with commas
  • Using curly braces incorrectly
3. What will be the output of this bash script?
colors=(red green blue)
echo ${colors[1]}
medium
A. green
B. red
C. blue
D. colors[1]

Solution

  1. Step 1: Understand array indexing in bash

    Bash arrays start at index 0, so colors[0] = red, colors[1] = green.
  2. Step 2: Identify the echoed element

    The script echoes colors[1], which is green.
  3. Final Answer:

    green -> Option A
  4. Quick Check:

    Index 1 = green [OK]
Hint: Array indexes start at zero in bash [OK]
Common Mistakes:
  • Thinking index starts at 1
  • Printing the variable name instead of value
  • Confusing array syntax with other languages
4. Find the error in this bash array usage:
myarray=(one two three)
echo $myarray[2]
medium
A. Missing quotes around array items
B. Array declaration syntax is wrong
C. Incorrect way to access array element
D. No error, script runs fine

Solution

  1. Step 1: Check array declaration

    Declaration with parentheses is correct: myarray=(one two three).
  2. Step 2: Check array element access

    Access must use braces: ${myarray[2]}, not $myarray[2].
  3. Final Answer:

    Incorrect way to access array element -> Option C
  4. Quick Check:

    Use ${array[index]} to access elements [OK]
Hint: Use braces ${} to access array elements [OK]
Common Mistakes:
  • Using $array[index] without braces
  • Adding commas in declaration
  • Forgetting parentheses in declaration
5. You want to store a list of filenames in an array and print each filename on a new line. Which script correctly does this?
hard
A. files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done
B. files=[file1.txt, file2.txt, file3.txt] for f in $files; do echo $f; done
C. files=(file1.txt file2.txt file3.txt) for f in $files; do echo $f; done
D. files={file1.txt file2.txt file3.txt} for f in ${files[@]}; do echo $f; done

Solution

  1. Step 1: Check array declaration

    files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done uses parentheses and space-separated items, which is correct.
  2. Step 2: Check loop syntax

    files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done uses for f in ${files[@]} to loop over all items correctly.
  3. Final Answer:

    files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done -> Option A
  4. Quick Check:

    Correct array and loop syntax = files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done [OK]
Hint: Use ${array[@]} to loop all items [OK]
Common Mistakes:
  • Using square brackets for arrays
  • Looping over $files instead of ${files[@]}
  • Using curly braces for array declaration
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