let's say I have an array that goes like this (a a b b b c c d) I want the array to have only one each character/string (a b c d) is there any way converting the array into lines so I can use sort & uniq without temp file?
thanks !
let's say I have an array that goes like this (a a b b b c c d) I want the array to have only one each character/string (a b c d) is there any way converting the array into lines so I can use sort & uniq without temp file?
thanks !
 
    
    You can use the printf command.
printf "%s\n" "${array[@]}" | sort | uniq
This will print each element of array followed by a newline to standard output.
To repopulate the array, you might use
readarray -t array < <(printf "%s\n" "${array[@]}" | sort | uniq)
However, you might instead simply use an associative array to filter out the duplicates.
declare -A aa
for x in "${array[@]}"; do
    aa[$x]=1
done
array=()
for x in "${!aa[@]}"; do
    array+=("$x")
done
 
    
    As a curiosity, the:
arr=(a a b b b c c d)
echo "${arr[@]}" | xargs -n1 | sort -u
prints
a
b
c
d
and the
echo "${arr[@]}" | xargs -n1 | sort -u | xargs
prints
a b c d
