Overview
Thearray 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:
Construction
Functions for creating arrays from various sources.array::from_string
array::from_string
Build array from delimited string.Parameters:
delimiter- Character or string separating elementsstring- Delimited string to splitarray_name- Optional: name of array variable to populate
array::from_lines
array::from_lines
Build array from newline-delimited string.
array::range
array::range
Generate range of integers.Parameters:
start- First number in rangeend- Last number in range (inclusive)step- Increment between numbers (default: 1)
Inspection
Functions for examining array properties.array::length
array::length
Get number of elements.
array::is_empty
array::is_empty
Check if array has no elements.Returns: Exit code 0 if empty, 1 otherwise
array::contains
array::contains
Check if array contains a value.Parameters:
- First argument: value to search for
- Remaining arguments: array elements
array::index_of
array::index_of
Find index of first matching element.Returns: Zero-based index, or -1 if not found
array::first
array::first
Get first element.
array::last
array::last
Get last element.
array::get
array::get
Get element at specific index.Parameters:
- First argument: zero-based index
- Remaining arguments: array elements
array::count_of
array::count_of
Count occurrences of a value.
Transformation
Functions for modifying arrays.array::print
array::print
Print each element on its own line (normalization helper).
array::reverse
array::reverse
Reverse element order.
array::flatten
array::flatten
Flatten by splitting each element on whitespace.
array::slice
array::slice
Extract subarray.Parameters:
start- Starting index (zero-based)length- Number of elements to extract- Remaining: array elements
array::push
array::push
Append element to end.
array::pop
array::pop
Remove last element.
array::unshift
array::unshift
Prepend element to beginning.
array::shift
array::shift
Remove first element.
array::remove_at
array::remove_at
Remove element at specific index.
array::remove
array::remove
Remove all occurrences of a value.
array::set
array::set
Replace element at index.Parameters:
index- Position to replacevalue- New value- Remaining: array elements
array::insert_at
array::insert_at
Insert element at index.If index is beyond end, element is appended.
Filtering
Functions for selecting subsets of elements.array::filter
array::filter
Keep only elements matching regex.Parameters:
- First argument: Bash regex pattern
- Remaining: array elements
array::reject
array::reject
Keep only elements NOT matching regex.
array::compact
array::compact
Remove empty elements.
Aggregation
Functions for combining or summarizing array elements.array::join
array::join
Join elements with delimiter.Parameters:
- First argument: delimiter string
- Remaining: elements to join
array::sum
array::sum
Sum all numeric elements.
array::min
array::min
Find minimum numeric value.
array::max
array::max
Find maximum numeric value.
Set operations
Functions for treating arrays as mathematical sets.Set operation functions take two space-separated strings as arguments, not individual elements.
array::intersect
array::intersect
Get elements present in both arrays.
array::diff
array::diff
Get elements in first array but not in second.
array::union
array::union
Get all unique elements from both arrays.
Sorting
Functions for ordering array elements.array::sort
array::sort
Sort elements alphabetically.Requires:
sortarray::sort::reverse
array::sort::reverse
Sort alphabetically in reverse.Requires:
sortarray::sort::numeric
array::sort::numeric
Sort numerically.Requires:
sortarray::sort::numeric_reverse
array::sort::numeric_reverse
Sort numerically in reverse.Requires:
sortAdvanced operations
array::equals
array::equals
Check if two arrays are identical (same elements, same order).Parameters: Two space-separated strings representing arraysReturns: Exit code 0 if equal, 1 otherwise
array::zip
array::zip
Pair elements from two arrays by index.Stops at the length of the shorter array.
array::rotate
array::rotate
Rotate array left by n positions.Parameters:
- First argument: number of positions to rotate
- Remaining: array elements
array::chunk
array::chunk
Split array into chunks of n elements.Each chunk is printed on one line, space-separated.Parameters:
- First argument: chunk size
- Remaining: array elements
array::unique
array::unique
Remove duplicate elements (preserves first occurrence order).
Requires Bash 4+ (uses associative arrays)
Usage patterns
Capturing array output
Since functions output elements line-by-line, capture withmapfile or readarray: