Skip to content

array

array

Operations on various lists, arrays and arguments. L_array_*

L_array_len

Get array length.

Example

L_array_len arr

Option: -v <var> Output variable

Argument: $1 <var array nameref

L_array_len_v

L_array_assign

Set elements of array.

Example

L_array_assign arr 1 2 3

Arguments:

  • $1 <var> array nameref
  • $@ elements to set

L_array_set

Assign element of an array

Example

L_array_assign arr 5 "Hello"

Arguments:

  • $1 <var> array nameref
  • $2 <int> array index
  • $3 <str> value to assign

L_array_append

Append elements to array.

Example

L_array_append arr "Hello" "World"

Arguments:

  • $1 <var> array nameref
  • $@ elements to append

L_array_insert

Insert element at specific position in an array.

This will move all elements from the position to the end of the array.

Example

L_array_insert arr 2 "Hello" "World"

Arguments:

  • $1 <var> array nameref
  • $2 <int> index position
  • $@ elements to append

L_array_pop_front

Remove first array element.

Argument: $1 <var> array nameref

L_array_pop_back

Remove last array element.

Example

L_array_pop_back arr

Argument: $1 <var> array nameref

L_array_is_dense

Return success, if all array elements are in sequence from 0.

Example

if L_array_is_dense arr; then echo "Array is dense"; fi

Argument: $1 <var> array nameref

L_array_prepend

Append elements to the front of the array.

Example

L_array_prepend arr "Hello" "World"

Arguments:

  • $1 <var> array nameref
  • $@ elements to append

L_array_clear

Clear an array.

Example

L_array_clear arr

Argument: $1 <var> array nameref

L_array_extract

Assign array elements to variables in order.

Example

  arr=("Hello" "World")
  L_array_extract arr var1 var2
  echo "$var1"  # prints Hello
  echo "$var2"  # prints World

Arguments:

  • $1 <var> array nameref
  • $@ variables to assign to

L_array_reverse

Reverse elements in an array.

Example

    arr=("world" "Hello")
    L_array_reverse arr
    echo "${arr[@]}"  # prints Hello world

Argument: $1 <var> array nameref

L_array_read

Wrapper for readarray for bash versions that do not have it.

Example

L_array_read arr <file

Options:

  • -d <str> separator to use, default: newline
  • -u <fd> file descriptor to read from
  • -s <int> skip first n lines

Argument: $1 <var> array nameref

L_array_pipe

Pipe an array to a command and then read back into an array.

Example

  arr=("Hello" "World")
  L_array_pipe arr tr '[:upper:]' '[:lower:]'
  echo "${arr[@]}"  # prints hello world

Option: -z Use null byte as separator instead of newline.

Arguments:

  • $1 array nameref
  • $@ command to pipe to

Shellcheck disable= SC2059

L_array_contains

check if array variable contains value

Example

  arr=("Hello" "World")
  L_array_contains arr "Hello"
  echo $?  # prints 0

Arguments:

  • $1 array nameref
  • $2 needle

L_array_filter_eval

Remove elements from array for which expression evaluates to failure.

Example

 arr=("Hello" "World")
 L_array_filter_eval arr '[[ "$1" == "Hello" ]]'
 echo "${arr[@]}"  # prints Hello

Arguments:

  • $1 array nameref
  • $2 expression to evaluate with array element of index L_i and value $1

L_array_join

Join array elements separated with the second argument.

Example

  arr=("Hello" "World")
  L_array_join -v res arr ", "
  echo "$res"  # prints Hello, World

Option: -v <var> Output variable

Arguments:

  • $1 <var> array nameref
  • $2 <str> string to join elements with

See: L_args_join

L_array_join_v

L_array_andjoin

Option: -v <var> Output variable

Argument: $1 <var> array nameref

See: L_args_andjoin

L_array_andjoin_v

L_array_to_string

Serialize an array to string representation.

Option: -v <var> Output variable

Argument: $1 <var> array nameref

See: L_array_from_string

L_array_to_string_v

L_array_from_string

Deserialize an array from string.

Example

  arr=(1 2 3)
  L_array_to_string -v tmp
  L_array_from_string arr2 "$tmp"
  echo "${arr2[@]}"

Arguments:

  • $1 <var> array nameref
  • $2 str result from L_array_to_string

See: L_array_to_string