For an array with arbitrary values, it's quite tricky with bash
as it doesn't have a builtin operator for that.
bash
however happens not to support storing NUL characters in its variables, so you can make use of that to pass that to other commands:
The equivalent of zsh
's:
new_array=("${(@u}array}")
on a recent GNU system, could be:
eval "new_array=($(
printf "%s\0" "${array[@]}" |
LC_ALL=C sort -zu |
xargs -r0 bash -c 'printf "%q\n" "$@"' sh
))"
Alternatively, with recent versions of bash
, you could use associated arrays:
unset hash
typeset -A hash
for i in "${array[@]}"; do
hash[$i]=
done
new_array=("${!hash[@]}")
The order of the elements would not be the same in those different solutions.