Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Accessing array elements in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Accessing array elements
Define array
↓
Access element by index
↓
Print or use element
↓
End
First, we create an array. Then we pick an element by its position number. Finally, we use or print that element.
Execution Sample
Bash Scripting
my_array=(apple banana cherry)
echo ${my_array[1]}
This script creates an array with three fruits and prints the second fruit.
Execution Table
StepActionArray StateIndex AccessedOutput
1Define array my_array[apple, banana, cherry]--
2Access element at index 1[apple, banana, cherry]1banana
3Print element[apple, banana, cherry]1banana
4End[apple, banana, cherry]--
💡 Script ends after printing the element at index 1.
Variable Tracker
VariableStartAfter 1Final
my_arrayundefined[apple, banana, cherry][apple, banana, cherry]
Key Moments - 2 Insights
Why does ${my_array[1]} print 'banana' and not 'apple'?
In bash arrays, indexing starts at 0. So index 0 is 'apple', index 1 is 'banana'. See execution_table row 2.
What happens if I try to access an index that does not exist?
Bash returns an empty string for out-of-range indexes. No error occurs. This is why we see no output if index is invalid.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
Abanana
Bapple
Ccherry
DNo output
💡 Hint
Check the 'Output' column in execution_table row 2.
At which step is the array first defined?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column for when the array is created.
If we change the index to 0 in ${my_array[0]}, what will be printed?
Abanana
Bapple
Ccherry
Dempty string
💡 Hint
Refer to variable_tracker for my_array contents and indexing starting at 0.
Concept Snapshot
Bash arrays start at index 0.
Use ${array[index]} to access elements.
Index must be a number.
Out-of-range index returns empty string.
Example: my_array=(a b c); echo ${my_array[1]} prints 'b'.
Full Transcript
We start by defining an array named my_array with three elements: apple, banana, and cherry. Then we access the element at index 1, which is banana, because bash arrays start counting from zero. Finally, we print the accessed element. If an index is out of range, bash returns an empty string without error. This simple flow helps us get any item from an array by its position.

Practice

(1/5)
1. In Bash scripting, how do you access the first element of an array named fruits?
easy
A. Using ${fruits[1]}
B. Using fruits[0]
C. Using $fruits[0]
D. Using ${fruits[0]}

Solution

  1. Step 1: Understand array indexing in Bash

    Array indexes start at 0, so the first element is at index 0.
  2. Step 2: Use correct syntax to access element

    In Bash, array elements are accessed with ${array_name[index]}. So, ${fruits[0]} gets the first element.
  3. Final Answer:

    Using ${fruits[0]} -> Option D
  4. Quick Check:

    First element = ${fruits[0]} [OK]
Hint: Remember arrays start at 0, use ${array[0]} for first element [OK]
Common Mistakes:
  • Using index 1 for first element
  • Omitting ${} around array access
  • Using $fruits[0] without braces
  • Treating array like a string variable
2. Which of the following is the correct syntax to print the third element of an array colors in Bash?
easy
A. echo ${colors(3)}
B. echo $colors[3]
C. echo ${colors[2]}
D. echo colors[2]

Solution

  1. Step 1: Identify correct index for third element

    Since indexing starts at 0, the third element is at index 2.
  2. Step 2: Use proper Bash array syntax

    The correct syntax is ${array_name[index]}. So, ${colors[2]} is correct.
  3. Final Answer:

    echo ${colors[2]} -> Option C
  4. Quick Check:

    Third element = ${colors[2]} [OK]
Hint: Use zero-based index and ${array[index]} syntax [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using 1-based index (3 instead of 2)
  • Missing ${} around array access
  • Printing array name as string
3. Given the Bash array declaration numbers=(10 20 30 40), what is the output of echo ${numbers[3]}?
medium
A. 40
B. 10
C. 30
D. Error

Solution

  1. Step 1: Identify the element at index 3

    Array numbers has elements: index 0=10, 1=20, 2=30, 3=40.
  2. Step 2: Access element at index 3

    ${numbers[3]} returns the element at index 3, which is 40.
  3. Final Answer:

    40 -> Option A
  4. Quick Check:

    numbers[3] = 40 [OK]
Hint: Index 3 means fourth element, here 40 [OK]
Common Mistakes:
  • Confusing index 3 with value 30
  • Expecting error for valid index
  • Using 1-based indexing
  • Printing entire array instead of element
4. What is wrong with this Bash code snippet?
my_array=(apple banana cherry)
echo ${my_array[3]}
medium
A. Index 3 is out of range, so output is empty string
B. Syntax error due to missing quotes
C. Array elements must be accessed with parentheses
D. It will print 'cherry' instead of empty

Solution

  1. Step 1: Check array size and index used

    Array has 3 elements at indexes 0,1,2. Index 3 is out of range.
  2. Step 2: Understand Bash behavior for out-of-range index

    Accessing an out-of-range index returns an empty string, no error occurs.
  3. Final Answer:

    Index 3 is out of range, so output is empty string -> Option A
  4. Quick Check:

    Out-of-range index returns empty string [OK]
Hint: Out-of-range indexes return empty, not errors [OK]
Common Mistakes:
  • Expecting syntax error for out-of-range index
  • Using parentheses instead of brackets
  • Assuming last element is at index 3
  • Expecting output 'cherry' for index 3
5. You have a Bash array items=(pen notebook eraser). You want to print the last element without hardcoding the index. Which command achieves this?
hard
A. echo ${items[-1]}
B. echo ${items[${#items[@]}-1]}
C. echo ${items[${#items[@]}]}
D. echo ${items[3]}

Solution

  1. Step 1: Understand array length and indexing

    ${#items[@]} gives the number of elements (3). Last index is length minus 1 (2).
  2. Step 2: Use arithmetic inside array index

    Use ${items[${#items[@]}-1]} to get last element dynamically.
  3. Step 3: Check other options

    echo ${items[-1]} uses negative index which Bash does not support. echo ${items[${#items[@]}]} uses length as index (out of range). echo ${items[3]} hardcodes index 3 which is out of range.
  4. Final Answer:

    echo ${items[${#items[@]}-1]} -> Option B
  5. Quick Check:

    Last element = items[length-1] [OK]
Hint: Use ${array[${#array[@]}-1]} for last element [OK]
Common Mistakes:
  • Using negative indexes (not supported in Bash)
  • Using length as index (off by one)
  • Hardcoding index without checking length
  • Forgetting to use ${} around 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