Skip to content

Instantly share code, notes, and snippets.

@kallookoo
Last active March 15, 2025 10:55
Show Gist options
  • Save kallookoo/72ed031a07d78e293a7bebe2f901a215 to your computer and use it in GitHub Desktop.
Save kallookoo/72ed031a07d78e293a7bebe2f901a215 to your computer and use it in GitHub Desktop.
Shell function to check if an element exists in an array
# in_array - Check if an element exists in an array
# Usage: in_array "needle" "${array[@]}"
# Example: in_array "apple" "${fruits[@]}"
in_array() {
[ $# -gt 1 ] && [ "$(printf '%s\n' "$@" | grep -cx -- "$1")" -gt "1" ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment