Fill

array_fill

Fill a named array with a value repeated n times. Replaces any existing contents.

Example

array_fill myarr x 3
printf '%s\n' "${myarr[@]}"
# => x
# => x
# => x

Arguments

  • $1 (string): Name of the array variable.
  • $2 (string): The value to fill with.
  • $3 (int): The number of times to repeat the value.

Exit codes

  • 0: Always

array_pad

Pad a named array to a minimum length by appending a fill value. Has no effect if the array is already at or above the minimum length.

Example

myarr=( a b c )
array_pad myarr 5 x
printf '%s\n' "${myarr[@]}"
# => a
# => b
# => c
# => x
# => x

Arguments

  • $1 (string): Name of the array variable.
  • $2 (int): The minimum length to pad the array to.
  • $3 (string): The value to pad with.

Exit codes

  • 0: Always

array_range

Generate a named array of integers from start to end, with optional step.

Example

array_range myarr 1 5
printf '%s\n' "${myarr[@]}"
# => 1
# => 2
# => 3
# => 4
# => 5

Arguments

  • $1 (string): Name of the array variable.
  • $2 (int): Start of the range (inclusive).
  • $3 (int): End of the range (inclusive).
  • $4 (int): Step between values (default: 1).

Exit codes

  • 0: Always