In bash
and most other Bourne-like shells, when you leave a variable expansion unquoted, e.g. $VotePedro
, the following steps are performed:
- Look up the value of the variable.
- Split the value at each block of whitespace into a list of strings. More generally, the separators are the characters in the value of the
IFS
variable; by default that's space, tab and newline.
- Interpret each element of the list as a wildcard pattern; for each element, if the pattern matches some files, then replace that element by the list of matching file names.
Thus you can split a string into whitespace-delimited elements (assuming the default value of IFS
) by turning off wildcard expansion and expanding a variable whose value is that string outside of quotes.
VotePedro="Vote for Pedro"
set -f
votePedroArray=($VotePedro)
set +f
for i in "${votePedroArray[@]}"; do …
You can directly do the split at the point of use; this would work even in shells such as sh that don't have arrays:
VotePedro="Vote for Pedro"
set -f
for i in ${votePedro}; do
set +f
…
done
set +f