bash 尝试将字符串拆分为两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20144593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to split a string into two variables
提问by coda
I'm trying to split a string into two variables (without having to use a while loop):
我正在尝试将字符串拆分为两个变量(无需使用 while 循环):
var="hello:world"
IFS=':' read var1 var2 <<< $var
echo "var1: $var1"
echo "var2: $var2"
but i'm not getting the desired result:
但我没有得到想要的结果:
var1: 'hello world'
var2: ''
Could anybody please explain if it's possible to do it this way (or similar way)?
有人可以解释一下是否可以以这种方式(或类似方式)做到这一点?
回答by fedorqui 'SO stop harming'
This is a bug in Bash 4.2. See chepner's answerfor a proper explanation.
这是 Bash 4.2 中的一个错误。有关正确解释,请参阅chepner 的回答。
It is about quotes. Use:
这是关于报价。用:
IFS=':' read var1 var2 <<< "$var"
^ ^
instead of
代替
IFS=':' read var1 var2 <<< $var
See result:
查看结果:
$ IFS=':' read var1 var2 <<< "$var"
$ echo "var1=$var1, var2=$var2"
var1=hello, var2=world
But
但
$ IFS=':' read var1 var2 <<< $var
$ echo "var1=$var1, var2=$var2"
var1=hello world, var2=
回答by chepner
FYI for future readers:
供未来读者参考:
After discussing this with the developers, it appears this is indeed a bug in bash
4.2, and has been fixed in the upcoming 4.3 release. From the devel branch change log
与开发人员讨论后,这似乎确实是bash
4.2 中的错误,并已在即将发布的 4.3 版本中修复。从开发分支更改日志
rrrr. Fixed several problems with IFS when it appears in the temporary environment and is used in redirections.
嗯。修复了 IFS 出现在临时环境中并用于重定向时的几个问题。
Although it's always a good idea to quote parameter expansions anyway, the OP's code should work as intended without quotes.
尽管无论如何引用参数扩展总是一个好主意,但 OP 的代码应该在没有引号的情况下按预期工作。
Here's an explanation of the bug. With the code
这是对错误的解释。随着代码
var="hello:world"
IFS=':' read var1 var2 <<< $var
the unquoted $var
shouldbe a single word, since it contains no character in the global value of IFS
(that is, no white-space). read
should then see the string hello:world
. Because it received two arguments, it should apply word-splitting using its local value of IFS
, producing hello
and world
which are assigned to var1
and var2
, respectively.
未加引号的$var
应该是单个单词,因为它的全局值中不包含任何字符IFS
(即,没有空格)。read
然后应该看到字符串hello:world
。因为它接收了两个参数,所以它应该使用其本地值IFS
、产生hello
和world
分别分配给var1
和来应用分词var2
。
The bug is that the here string appears to undergo partial splitting using the "leaked" value of IFS
being passed to read
. As a result, the string becomes hello world
, but is still seen by read
as a single word. Since that word does not contain a :
, read
does not split it into two words, and the entire string is assigned to var1
.
错误在于 here 字符串似乎使用IFS
传递给的“泄漏”值进行了部分拆分read
。结果,字符串变为hello world
,但仍被视为read
单个单词。由于该单词不包含 a :
,因此read
不会将其拆分为两个单词,而是将整个字符串分配给var1
。
In bash
4.3, as documented, the expansion of $var
does not undergo word-splitting as the argument to the <<<
operator; the code
在bash
4.3 中,如文档所述, 的扩展$var
不会作为<<<
运算符的参数进行分词;编码
var="hello:1:2 world"
IFS=: read var1 var2 <<< $var
sets var1
to hello
and var2
to 1:2 world
.
套var1
到hello
和var2
到1:2 world
。