Slice

array_slice

Print a slice of array elements using Python-style index notation. Supports single index, range, and step variants.

Example

array_slice 2 myarr        # prints element at index 2
array_slice :3 myarr       # prints elements at indices 0, 1, 2
array_slice ::2 myarr      # prints every 2nd element from index 0
array_slice 1:4 myarr      # prints elements at indices 1, 2, 3
array_slice 0:6:2 myarr    # prints every 2nd element between indices 0 and 6

Arguments

  • $1 (string): Slice specifier: n, :n, ::n, x:y, or x:y:z
  • $2 (string): Name of the array variable (passed by name, not value).

Exit codes

  • 0: Always

Output on stdout

  • Matching elements, one per line.

array_at

Print the element at index n in a named array. Supports negative indices (from end). Pass --random to select a random element.

Example

myarr=( a b c d e )
array_at myarr -1     # => e
array_at myarr --random  # => (random element)

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): Index (supports negative), or --random for a random element.

Exit codes

  • 0: Always

Output on stdout

  • The element at the given index.

array_head

Print the first n elements of a named array.

Arguments

  • $1 (string): Name of the array variable.
  • $2 (int): Number of elements to print from the start (default: 1).

Exit codes

  • 0: Always

Output on stdout

  • The first n elements, one per line.

array_tail

Print the last n elements of a named array.

Arguments

  • $1 (string): Name of the array variable.
  • $2 (int): Number of elements to print from the end (default: 1).

Exit codes

  • 0: Always

Output on stdout

  • The last n elements, one per line.