Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Indexed array declaration in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Indexed array declaration
Start script
↓
Declare indexed array
↓
Assign values to array elements
↓
Access or print array elements
↓
End script
This flow shows how a bash script declares an indexed array, assigns values, and accesses them.
Execution Sample
Bash Scripting
fruits=("apple" "banana" "cherry")
echo ${fruits[0]}
echo ${fruits[1]}
echo ${fruits[2]}
Declare an indexed array named fruits with three elements and print each element.
Execution Table
StepActionArray StateOutput
1Declare array fruits with elements apple, banana, cherry["apple", "banana", "cherry"]
2Print fruits[0]["apple", "banana", "cherry"]apple
3Print fruits[1]["apple", "banana", "cherry"]banana
4Print fruits[2]["apple", "banana", "cherry"]cherry
5End of script["apple", "banana", "cherry"]
💡 All array elements accessed and printed, script ends.
Variable Tracker
VariableStartAfter DeclarationFinal
fruitsundefined["apple", "banana", "cherry"]["apple", "banana", "cherry"]
Key Moments - 2 Insights
Why do array indices start at 0 in bash?
Bash arrays are zero-indexed, so the first element is at index 0 as shown in execution_table steps 2-4.
What happens if I try to access an index that was not assigned?
Accessing an unassigned index returns an empty string, because only declared indices hold values, as seen in the array state in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 3?
Abanana
Bapple
Ccherry
Dundefined
💡 Hint
Check the Output column for step 3 in the execution_table.
At which step is the array fruits declared?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Action column to find when the array is first created.
If you add a new element fruits[3]="date", what will be the array state after declaration?
A["apple", "banana", "cherry"]
B["apple", "banana", "cherry", "date"]
C["date"]
Dundefined
💡 Hint
Adding an element at index 3 appends it to the array, expanding its state.
Concept Snapshot
Indexed array declaration in bash:
fruits=("apple" "banana" "cherry")
Access elements by index: ${fruits[0]}
Indices start at 0
Unassigned indices return empty
Use parentheses to declare arrays
Full Transcript
This lesson shows how to declare an indexed array in bash scripting. The array named fruits is created with three elements: apple, banana, and cherry. Each element is accessed by its index starting at zero. The script prints each element in order. The array state remains the same throughout the script. Beginners often wonder why indices start at zero and what happens if an index is not assigned. The answer is that bash arrays are zero-indexed and unassigned indices return empty strings. Adding new elements expands the array. This visual trace helps understand how arrays work step-by-step.

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