For string-to-array comparison the following example uses space as the separator and expects it to be at the start and end of the list so that every value will be wrapped between two separators. It also avoids using an external process or subshell.
for ((index=0; index < "$((${#array[@]}"; index++)); do
if [[ "$array_string" != *" ${array[$index]} "* ]]; then
return 1
fi
done
For an array-to-array comparison it's easy to convert the string to an array after removing the leading separator, and then use a nested loop to compare them. This can also avoid using an external process or subshell as read is a bash builtin:
# Needs to read to a different variable so it
# doesn't empty the source and read nothing.
IFS=' ' read -ra string_array <<< "${array_string# }"
found=1
for ((x=0; x < "${#string_array[@]}"; x++)); do
for ((y=0; y < "${#array[@]}"; y++)); do
if [[ "${string_array[$x]}" == "${array[$y]}" ]]; then
found=0
fi
done
if [[ found -ne 0 ]]; then
return 1
fi
found=1
done
array_stringto an array, then use nested loops to test if they're all inarray.bashis not the right language to use.array_stringexpected to contain multiple words but stored in a variable? What if one of the word contains spaces? This design itself is flawed. You should be maintaining two separate arrays