Bash IFS not splitting string correctly

473 views Asked by At

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?

1

There are 1 answers

0
chepner On BEST ANSWER

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