Search code examples
bashifs

Bash IFS not splitting string correctly


I am having trouble getting IFS to split the string correctly based on the colon delimiter. It seems that the -e inside the string is being considered as an option instead of being considered as a literal string.

#!/bin/bash
string_val="-e:SQA"

IFS=: read -a items <<< "$string_val"

echo "${items[0]}" # Prints empty value
echo "${items[1]}" # Prints SQA

How can this be fixed?


Solution

  • The string is being split correctly; the -e in ${items[0]} is treated as an option to echo.

    $ string_val="-e:SQA"
    $ IFS=: read -a items <<< "$string_val"
    $ printf '%s\n' "${items[0]}"
    -e