If condition with array

hi,

say if my array has the following integer values.

array=( 5 10 15 20 )

and A assigns to 5.

A=5

How do I if condition to check against if value A is in this array or not, in shell script?

thx

I'd make a helper function:

 in_array() {
   local n=$1 h
  
   shift
   for h; do
     [[ $n = "$h" ]] && return
   done
  
   return 1
 }
  
 if in_array "$A" "${array[@]}"; then
   ... do something about it ...
 fi
 

That's a nice function that hides the loop complexity!
--
bash-4 has associative arrays.
The assignment needs a loop (I think), but the lookup is easy

declare -A ARRAY
# assignment
for i in 5 10 15 20
do
  ARRAY[$i]=
done
A=5
# lookup
if [ -n "${ARRAY[$A]+x}" ]; then
  ... found, do something
fi

Try also

while [[ ($i -lt ${#array[@]}) && (${array[$i]} != $A) ]]; do (( i++)); done ; echo $(( $i == ${#array[@]} ))
for i in "${array[@]}"
do
  if [ "$i" = 5 ]; then
    echo yes
    break
  fi
done

For a numerical comparison, replace [ "$i" = 5 ] with [ "$i" -eq 5 ]

thanks all. got it working now.