Skip to content

Instantly share code, notes, and snippets.

@anderseknert
Created February 15, 2013 01:12
Show Gist options
  • Save anderseknert/4957901 to your computer and use it in GitHub Desktop.
Save anderseknert/4957901 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# As passing an array by value is not possible (that I know) this function
# requires that the array passed has been expanded (@)
function in_array() {
# Require both needle and haystack
if [ -z "$1" -o -z "$2" ]; then
return;
fi
# Use the first argument as needle
local needle="${1}"
# Then shift the arguments so that only the haystack remains
shift
# Create a new array from all remaining arguments
local array=(`echo "${@}"`)
local i
# Iterate through the array and return true (0) on match
for i in "${array[@]}"
do
if [ "${i}" = "${needle}" ]; then
return 0
fi
done
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment