The array module implements functions for sorting, searching, printing, and comparing arrays.
The array module does not depend on other modules.
Function | Purpose |
---|---|
array_contains() | Check if an element is in an array |
array_identical() | Check if two arrays are identical |
array_same() | Check if two arrays contain the same elements |
array_sort() | Sort an array |
array_to_lines() | Print an array, one element per line |
Check whether an element is contained in an array.
array_contains "$element" "${array[@]}"
The array_contains()
function checks whether element
is contained in array
.
It does so by iterating over the array, comparing each element in the array with
element
.
Return value | Meaning |
---|---|
0 | The element is contained in the array |
1 | The element was not found in the array |
array_contains()
does not read from standard input.
array_contains()
does not write to standard output.
array_contains()
does not write to standard error.
Checks if two arrays are identical.
array_identical left right
The array_identical()
function checks whether the elements in the arrays
referenced by left
and right
are identical. That means, it checks if the
two arrays contain the same elements in the same order.
The parameters left
and right
are name references to the arrays to be
compared.
Return value | Meaning |
---|---|
0 | The arrays are identical |
1 | The arrays are not identical |
array_identical()
does not read from standard input.
array_identical()
does not write to standard output.
array_identical()
does not write to standard error.
Check if two arrays contain the same elements.
array_same left right
The array_same()
function checks whether the arrays referenced by left
and right
contain the same elements. That means, it checks if the arrays
are permutations.
The parameters left
and right
are name references to the arrays to be
compared.
Return value | Meaning |
---|---|
0 | The arrays contain the same elements |
1 | The arrays do not contain the same elements |
array_same()
does not read from standard input.
array_same()
does not write to standard output.
array_same()
does not write to standard error.
Sort an array.
array_sort "${array[@]}"
The array_sort()
function sorts the elements of array
and outputs the
result to standard output. The elements are sorted using a natural number
sort, similar to the sort -V
command from GNU Coreutils.
If the array is empty, nothing will be written to standard output.
Return value | Meaning |
---|---|
0 | Success (always returned) |
array_sort()
does not read from standard input.
The sorted array is written to standard output, one element per line.
array_sort()
does not write to standard error.
Print an array, one element per line.
array_to_lines "${array[@]}"
The array_to_lines()
function outputs array
to standard output, writing
each element in the array to a separate line. The elements of the array are
output as-is, meaning that an empty string in the array will cause an empty
line to be written. The elements will be written in their order in the array.
If the array is empty, nothing will be written to standard output.
Return value | Meaning |
---|---|
0 | Success (always returned) |
array_to_lines()
does not read from standard input.
array_to_lines()
writes one line per array element to standard output. If
the array is empty, nothing will be written.
array_to_lines()
does not write to standard error.