简体   繁体   English

Bash IFS无法正确分割字符串

[英]Bash IFS not splitting string correctly

I am having trouble getting IFS to split the string correctly based on the colon delimiter. 我无法让IFS根据冒号分隔符正确分割字符串。 It seems that the -e inside the string is being considered as an option instead of being considered as a literal string. 似乎字符串内的-e被视为一个选项,而不是被视为文字字符串。

#!/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 . ${items[0]}-e被视为echo的选项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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