Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active December 20, 2015 11:09
Show Gist options
  • Save jtpaasch/6121089 to your computer and use it in GitHub Desktop.
Save jtpaasch/6121089 to your computer and use it in GitHub Desktop.
A bash function to check if an item is duplicated in a list. That is to say, it checks to make sure it is not already in the list.
#!/bin/bash
# A function that checks if an item is not already in a list.
# The first argument is the item to check for, the rest are the list to check in.
# Returns 1 if the item is duplicated in the list, or 0 if it's not.
not_duplicated() {
# The first argument is the item to check for.
local ITEM=$1
# The rest are the list to check in.
local LIST="${@:2}"
# If we find the item in the list, return 1.
for I in ${LIST[@]}
do
if [[ $I == $ITEM ]]; then
return 1
fi
done
# Otherwise, we're good.
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment