If none of your array elements includes a newline character, you can do this:
mapfile -t uniq < <(printf "%s\n" "${RUN_Arr[@]}" | sort -u)
if (( ${#uniq[@]} > 1 )); then
echo "The elements are not the same: ${uniq[@]}"
# ...
If you need to protect against elements with newline characters, there is a simple solution if you have bash 4.4 (for the -d
option) and Gnu or FreeBSD sort (for the -z
option):
mapfile -d '' -t uniq < <(printf "%s\n" "${RUN_Arr[@]}" | sort -zu)
if (( ${#uniq[@]} > 1 )); then
echo "The elements are not the same: ${uniq[@]}"
exit 1
fi
Without bash 4.4, you could use an adaptation of @hunteke's answer:
for i in "${RUN_Arr[@]:1}"; do
if [[ $i != ${RUN_ARR[0]} ]]; then
printf "The elements are not the same:"
printf "%s\0" "${RUN_Arr[@]}" |
sort -zu |
xargs -0 printf " %s"
printf "\n"
exit 1
fi
done
(This still requires a sort which supports -z
.)