Skip to content

Check a Named Array is Non-Empty

Return success if the named array exists and contains at least one element, using a nameref so callers pass the array name.

Language: Shell

Added on: 19th August 2026

check_array() {
  local arr_name=$1

  declare -p "$arr_name" &>/dev/null || return 1

  local -n arr=$arr_name
  (( ${#arr[@]} > 0 ))
}

Caveats

Pass the array name, not its value. Needs bash nameref (4.3+).