Search code examples
linuxbashquotessplitifs

Linux bash string split using IFS on single quote


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?


Solution

  • When you do:

    for token in ${stringTokens[@]}
    

    The loop effectively becomes:

    for token in a string with some single quotes in it
    

    The for loop does not parse the array element-wise, but it parses the entire output of the string separated by spaces.

    Instead try:

    for token in "${stringTokens[@]}";
    do
       echo "$token"
    done
    

    This will be equivalent to:

    for token in "in a string with some " "single" " quotes in it"
    

    Output on my PC:

    a string with some 
    single
     quotes in it
    

    Check this out for more Bash Pitfalls: http://mywiki.wooledge.org/BashPitfalls