Contains

array_index

Print the index of the first element that exactly matches a value.

Example

myarr=( a b c )
array_index myarr b  # => 1

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): The value to search for.

Exit codes

  • 0: Element found.
  • 1: Element not found.

Output on stdout

  • The zero-based index of the first matching element.

array_some

Return 0 if any element of a named array matches a glob pattern.

Example

myarr=( apple banana cherry )
array_some myarr 'ban*'  # => 0

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): Glob pattern to test against each element.

Exit codes

  • 0: At least one element matched.
  • 1: No elements matched.

array_every

Return 0 if every element of a named array matches a glob pattern.

Example

myarr=( apple apricot avocado )
array_every myarr 'a*'  # => 0

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): Glob pattern that all elements must match.

Exit codes

  • 0: All elements matched.
  • 1: At least one element did not match.

array_last_index

Print the last index of an element that exactly matches a value.

Example

myarr=( a b c b d )
array_last_index myarr b  # => 3

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): The value to search for.

Exit codes

  • 0: Element found.
  • 1: Element not found.

Output on stdout

  • The zero-based index of the last matching element.

array_find

Print the first element matching a glob pattern.

Example

myarr=( apple banana cherry )
array_find myarr 'b*'  # => banana

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): Glob pattern to match against.

Exit codes

  • 0: A match was found.
  • 1: No match found.

Output on stdout

  • The first matching element.

array_find_last

Print the last element matching a glob pattern.

Example

myarr=( apple banana cherry apricot )
array_find_last myarr 'a*'  # => apricot

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): Glob pattern to match against.

Exit codes

  • 0: A match was found.
  • 1: No match found.

Output on stdout

  • The last matching element.