From: Matthias Kruk Date: Wed, 24 Mar 2021 22:24:30 +0000 (+0900) Subject: include/array: Add function for checking if an element is in an array X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=1b9ef1089ad599a98791baefa825c6f2b6925724;p=toolbox include/array: Add function for checking if an element is in an array Bash doesn't provide a simple way to check if an element is in an array. This commit adds the array_contains convenience function to solve that problem. --- diff --git a/include/array.sh b/include/array.sh new file mode 100644 index 0000000..2ff4b28 --- /dev/null +++ b/include/array.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +__init() { + return 0 +} + +array_contains() { + local needle + local haystack + + local cur + + needle="$1" + haystack=("${@:2}") + + for cur in "${haystack[@]}"; do + if [[ "$needle" == "$cur" ]]; then + return 0 + fi + done + + return 1 +}