Article 56VPA How to check if array has values?

How to check if array has values?

by
ddenial
from LinuxQuestions.org on (#56VPA)
Hello all,

In my bash script, I want to check if array has values. If it does, I want to pass it to a function.

This is a sample code of that.
Code:#!/bin/bash

declare -a array=( "This is a" "test" "of something" )

if [[ -n "${array}" ]] ; then
echo "Has content"
else
echo "Hmmmm"
fiThe works fine. But if a check with shellcheck, it throws error
Code:$ shellcheck t2.sh

In t2.sh line 5:
if [[ -n "${array}" ]] ; then
^------^ SC2128: Expanding an array without an index only gives the first element.

For more information:
https://www.shellcheck.net/wiki/SC2128 -- Expanding an array without an ind...Visiting the error page https://www.shellcheck.net/wiki/SC2128 doesnt help either. It just says that by using this way I'll only get first value in array. Thats OK, I just want to check if its null.

Is there another way to check if array is null. This should also satisfy shellcheck.

Thankslatest?d=yIl2AUoC8zA latest?i=y2dX_gjF3Js:ZnJFxknGr8w:F7zBnMy latest?i=y2dX_gjF3Js:ZnJFxknGr8w:V_sGLiP latest?d=qj6IDK7rITs latest?i=y2dX_gjF3Js:ZnJFxknGr8w:gIN9vFwy2dX_gjF3Js
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments