Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Created December 3, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavxyz/934c99968dac7d00a9cb649b0319a0da to your computer and use it in GitHub Desktop.
Save stavxyz/934c99968dac7d00a9cb649b0319a0da to your computer and use it in GitHub Desktop.
Does the bash array include the item?
function hasItem() {
local _item=${1}
# local _array=("$@")
local _array=("${@:2}")
echo "Looking at array --> ${_array[*]}"
for i in "${_array[@]}"; do
echo "Comparing ${i} to ${_item}"
if [[ $_item == "${i}" ]]; then
echo "yes"
return 0
fi
done
echo "no"
return 1
}
l1=( one two four )
if ! hasItem "three" "${l1[@]}"; then
echo "SUCCESS"
else
echo "FAIL"
fi
if hasItem "four" "${l1[@]}"; then
echo "SUCCESS"
else
echo "FAIL"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment