Skip to main content

Overview

The array module provides comprehensive array manipulation functions. Arrays are passed as individual arguments ("$@") since Bash cannot pass arrays by value. Total functions: 50+
Most functions output one element per line, making them pipeline-friendly. Capture results with:
mapfile -t result < <(array::function "$@")
# or for Bash 3:
IFS=$'\n' read -d '' -ra result < <(array::function "$@")

Construction

Functions for creating arrays from various sources.
Build array from delimited string.
array::from_string "," "a,b,c"
# Output:
# a
# b
# c

# Or populate a named array:
array::from_string "," "a,b,c" my_array
echo "${my_array[0]}" # a
Parameters:
  • delimiter - Character or string separating elements
  • string - Delimited string to split
  • array_name - Optional: name of array variable to populate
Build array from newline-delimited string.
array::from_lines "line1\nline2\nline3"
# Output:
# line1
# line2
# line3
Generate range of integers.
array::range 1 5
# Output: 1 2 3 4 5

array::range 0 10 2
# Output: 0 2 4 6 8 10
Parameters:
  • start - First number in range
  • end - Last number in range (inclusive)
  • step - Increment between numbers (default: 1)

Inspection

Functions for examining array properties.
Get number of elements.
array::length "a" "b" "c"
# Output: 3
Check if array has no elements.
if array::is_empty "$@"; then
  echo "Array is empty"
fi
Returns: Exit code 0 if empty, 1 otherwise
Check if array contains a value.
if array::contains "needle" "hay" "needle" "stack"; then
  echo "Found it!"
fi
Parameters:
  • First argument: value to search for
  • Remaining arguments: array elements
Returns: Exit code 0 if found, 1 otherwise
Find index of first matching element.
array::index_of "needle" "hay" "needle" "stack"
# Output: 1

array::index_of "missing" "hay" "needle" "stack"
# Output: -1
Returns: Zero-based index, or -1 if not found
Get first element.
array::first "a" "b" "c"
# Output: a
Get last element.
array::last "a" "b" "c"
# Output: c
Get element at specific index.
array::get 1 "a" "b" "c"
# Output: b
Parameters:
  • First argument: zero-based index
  • Remaining arguments: array elements
Count occurrences of a value.
array::count_of "foo" "foo" "bar" "foo" "baz"
# Output: 2

Transformation

Functions for modifying arrays.
Print each element on its own line (normalization helper).
array::print "a" "b" "c"
# Output:
# a
# b
# c
Reverse element order.
array::reverse "a" "b" "c"
# Output:
# c
# b
# a
Flatten by splitting each element on whitespace.
array::flatten "a" "b c" "d"
# Output:
# a
# b
# c
# d
Extract subarray.
array::slice 1 2 "a" "b" "c" "d"
# Output:
# b
# c
Parameters:
  • start - Starting index (zero-based)
  • length - Number of elements to extract
  • Remaining: array elements
Append element to end.
array::push "new" "a" "b" "c"
# Output:
# a
# b
# c
# new
Remove last element.
array::pop "a" "b" "c"
# Output:
# a
# b
Prepend element to beginning.
array::unshift "new" "a" "b" "c"
# Output:
# new
# a
# b
# c
Remove first element.
array::shift "a" "b" "c"
# Output:
# b
# c
Remove element at specific index.
array::remove_at 1 "a" "b" "c"
# Output:
# a
# c
Remove all occurrences of a value.
array::remove "b" "a" "b" "c" "b"
# Output:
# a
# c
Replace element at index.
array::set 1 "X" "a" "b" "c"
# Output:
# a
# X
# c
Parameters:
  • index - Position to replace
  • value - New value
  • Remaining: array elements
Insert element at index.
array::insert_at 1 "X" "a" "b" "c"
# Output:
# a
# X
# b
# c
If index is beyond end, element is appended.

Filtering

Functions for selecting subsets of elements.
Keep only elements matching regex.
array::filter "^[0-9]+$" "foo" "123" "bar" "456"
# Output:
# 123
# 456
Parameters:
  • First argument: Bash regex pattern
  • Remaining: array elements
Keep only elements NOT matching regex.
array::reject "^[0-9]+$" "foo" "123" "bar" "456"
# Output:
# foo
# bar
Remove empty elements.
array::compact "a" "" "b" "" "c"
# Output:
# a
# b
# c

Aggregation

Functions for combining or summarizing array elements.
Join elements with delimiter.
array::join "," "a" "b" "c"
# Output: a,b,c
Parameters:
  • First argument: delimiter string
  • Remaining: elements to join
Sum all numeric elements.
array::sum 10 20 30
# Output: 60
Find minimum numeric value.
array::min 5 2 8 1 9
# Output: 1
Find maximum numeric value.
array::max 5 2 8 1 9
# Output: 9

Set operations

Functions for treating arrays as mathematical sets.
Set operation functions take two space-separated strings as arguments, not individual elements.
Get elements present in both arrays.
array::intersect "a b c" "b c d"
# Output:
# b
# c
Get elements in first array but not in second.
array::diff "a b c" "b c d"
# Output:
# a
Get all unique elements from both arrays.
array::union "a b" "b c"
# Output:
# a
# b
# c

Sorting

Functions for ordering array elements.
Sort elements alphabetically.
array::sort "cherry" "apple" "banana"
# Output:
# apple
# banana
# cherry
Requires: sort
Sort alphabetically in reverse.
array::sort::reverse "cherry" "apple" "banana"
# Output:
# cherry
# banana
# apple
Requires: sort
Sort numerically.
array::sort::numeric 10 2 30 1
# Output:
# 1
# 2
# 10
# 30
Requires: sort
Sort numerically in reverse.
array::sort::numeric_reverse 10 2 30 1
# Output:
# 30
# 10
# 2
# 1
Requires: sort

Advanced operations

Check if two arrays are identical (same elements, same order).
if array::equals "a b c" "a b c"; then
  echo "Arrays are equal"
fi
Parameters: Two space-separated strings representing arraysReturns: Exit code 0 if equal, 1 otherwise
Pair elements from two arrays by index.
array::zip "a b c" "1 2 3"
# Output:
# a 1
# b 2
# c 3
Stops at the length of the shorter array.
Rotate array left by n positions.
array::rotate 2 "a" "b" "c" "d" "e"
# Output:
# c
# d
# e
# a
# b
Parameters:
  • First argument: number of positions to rotate
  • Remaining: array elements
Split array into chunks of n elements.
array::chunk 2 "a" "b" "c" "d" "e"
# Output:
# a b
# c d
# e
Each chunk is printed on one line, space-separated.Parameters:
  • First argument: chunk size
  • Remaining: array elements
Remove duplicate elements (preserves first occurrence order).
array::unique "a" "b" "a" "c" "b" "d"
# Output:
# a
# b
# c
# d
Requires Bash 4+ (uses associative arrays)

Usage patterns

Capturing array output

Since functions output elements line-by-line, capture with mapfile or readarray:
# Bash 4+
mapfile -t filtered < <(array::filter "^[0-9]+$" "${items[@]}")

# Bash 3 compatible
IFS=$'\n' read -d '' -ra filtered < <(array::filter "^[0-9]+$" "${items[@]}")

# Or use a while loop
while IFS= read -r item; do
  # process each item
done < <(array::filter "^[0-9]+$" "${items[@]}")

Pipeline composition

Chain multiple operations:
array::range 1 100 | \
  array::filter "^[0-9]+$" | \
  array::unique | \
  array::sort::numeric

Working with files

# Read lines from file into array
mapfile -t lines < file.txt

# Process and save
array::sort "${lines[@]}" > sorted.txt

Build docs developers (and LLMs) love