Skip to content

Instantly share code, notes, and snippets.

@snown
Created March 26, 2020 22:12
Show Gist options
  • Save snown/46e00cd0516497233c78d7114105531f to your computer and use it in GitHub Desktop.
Save snown/46e00cd0516497233c78d7114105531f to your computer and use it in GitHub Desktop.
Get unique elements in Bash
function uniqueElements {
local input_array=( "$@" )
local IFS=$'\n'
local RESULT=( $(printf '%s\n' ${input_array[@]} | awk '!seen[$0]++') )
# Uncomment if you want the result to come back in a format that can easily be passed to `declare -a`
#echo $(declare -p RESULT | sed "s/^[^(]*// ; s/'$//")
echo "${RESULT[@]}"
}
# Run if not sourced
if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then
uniqueElements "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment