I was trying to check if an array contained a value and I found Check if a Bash array contains a value
It did not work for me, and it was because I decided to remove the spaces surrounding the quotes, like this:
words=(aa bb cc)
[[ " ${words[@]} " =~ " a " ]] && echo "YES" || echo "NO"; # This is the real answer, and it works
------
[[ "${words[@]}" =~ "a" ]] && echo "YES" || echo "NO"; # This does not work. Why???
- Whats the difference when you surround them with spaces or when you dont?
And for my curiosity. In the previous question I mentioned, some answers/comments go with:
${array[*]}
, and others with ${array[@]}
- Do they both "iterate" through the loop in the same way?