bash adding filenames from directory to declared array

Software Development

Counting in Bash

To count in a Bash for loop, you can use several different syntax styles depending on your specific needs.

Method 1: Using C-Style Syntax

This is the most flexible and readable way to count if you are familiar with languages like C, Java, or JavaScript. It allows you to easily control the start, end, and step size.

# Initialize counter
count=1

# Loop over items (e.g., files in an array)
for file in "${my_files[@]}"; do
    echo "Processing file #$count: $file"

    # Increment the counter
    ((count++))
done

Method 2: Using mapfile / readarray with find

If you need to filter files (e.g., only regular files, no directories) or expect complex filenames with unusual characters like newlines, use this method. It requires Bash 4.4 or newer.

# Declare the array
declare -a my_files

# Use mapfile combined with a null-terminated find command
mapfile -d ' ' my_files < <(find /path/to/directory -maxdepth 1 -type f -print0)

Method 3: Using a while loop

If you are working on an older legacy system that doesn't support the newer mapfile flags, a custom while loop is the bulletproof alternative.

declare -a my_files

while IFS= read -r -d ':' file; do
    my_files+=("$file")
done < <(find /path/to/directory -maxdepth 1 -type f -print0)

To make sure your array was populated correctly, use these standard calls:

  • Print all elements safely: printf '%s\n' "${my_files[@]}"
  • Get the total count of elements: echo "${#my_files[@]}"
  • Access a specific element (e.g., the first one at index 0): echo "${my_files[0]}"

Avoid using loops like my_files=($(ls directory)) as utilizing the output of ls breaks easily when filenames contain spaces or special characters.

Working with Array Indices in Bash

To work with array indices in Bash, you can use specific syntax to access keys, count elements, or loop through the array by its index numbers.

Method 1: Getting All Array Indices (Keys)

If you need a list of all the active indices in an array, use the ${!array[@]} syntax (notice the exclamation mark). This is the best practice for both indexed and associative arrays.

# Declare and populate a sample array
my_array=("apple" "banana" "cherry")

# Print all indices (Output: 0 1 2)
echo "${!my_array[@]}"

Method 2: Looping Through an Array Using Indices

Combining the indices syntax with a for loop allows you to access both the index number and the value stored at that index simultaneously. This is highly recommended for manipulating specific elements.

my_array=("apple" "banana" "cherry")

# Loop through the indices
for i in "${!my_array[@]}"; do
    echo "Index: $i, Value: ${my_array[$i]}"
done

Method 3: Getting the Total Count of Elements

To find out how many elements are currently in your array, use the ${#array[@]} syntax.

my_array=("apple" "banana" "cherry")

# Print the total number of items (Output: 3)
echo "${#my_array[@]}"

Method 4: Accessing Indices from the End (Negative Indices)

Bash supports negative indexing to count backward from the end of the array. This requires Bash 4.3 or newer.

my_array=("apple" "banana" "cherry")

# Get the last element (Output: cherry)
echo "${my_array[-1]}"

# Get the second to last element (Output: banana)
echo "${my_array[-2]}"

Method 5: Handling Sparse Arrays (Gaps in Indices)

If you delete an element from an array using unset, Bash does not automatically shift the other elements to fill the gap. Accessing indices directly prevents errors in these sparse arrays.

my_array=("apple" "banana" "cherry")

# Delete the middle element
unset "my_array[1]"

# If you loop by index, it safely skips index 1:
for i in "${!my_array[@]}"; do
    echo "Index: $i contains ${my_array[$i]}"
done

# Output:
# Index: 0 contains apple
Back to all articles