Created
March 26, 2020 22:12
-
-
Save snown/46e00cd0516497233c78d7114105531f to your computer and use it in GitHub Desktop.
Get unique elements in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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