简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM