there are many answered questions about IFS string splitting and single quote escaping in Linux bash, but I found none joining the two topics. Stumbling upon the problem I got a strange (to me) behavior with a code like the one here below:
(bash script block)
theString="a string with some 'single' quotes in it"
old_IFS=$IFS
IFS=\'
read -a stringTokens <<< "$theString"
IFS=$old_IFS
for token in ${stringTokens[@]}
do
echo $token
done
# let's say $i holds the piece of string between quotes
echo ${stringTokens[$i]}
What happens is that the echo-ed element of the array actually contains the sub-string I need (thus leading me to think that the \' IFS is correct) while the for loop return the string split on spaces.
Can someone kindly help me understanding why the same array (or what in my mind looks like the same array) behaves like this?