Trying to split a string into two variables
Asked Answered
R

2

14

I'm trying to split a string into two variables (without having to use a while loop):

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)?

Revoke answered 22/11, 2013 at 12:10 Comment(2)
Note that this is not quite a duplicate; the code above is correct, but affected by a bug in 4.x (fixed in 4.3) that is not addressed in the linked duplicate.Myriapod
Based on that, the appropiate behaviour is to reopen this (I just did) and accepted chepner's answer instead.Dunite
D
25

This is a bug in Bash 4.2. See chepner's answer for a proper explanation.


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=
Dunite answered 22/11, 2013 at 12:11 Comment(7)
Great to read that, @coda, quoting is always important. Remember you can accept the answer if your question is already solved!Dunite
+1 This is one I don't understand (I'm not disputing it, just not understanding it). The setting of IFS should refer to the environment in which read is executed, but $var is expanded as part of the input redirection, which should precede the execution of read. Further, without quoting, it appears that the string is indeed one word, assigned to var1, but then split, as var1 gets hello and world, not hello:world.Myriapod
@Myriapod I found another funny behaviour: if you do IFS=':' read var1 var2 <<< hello:bye then it does split properly, while if it comes from a variable it does not.Dunite
Odd. I've filed a bug report, referencing this question. I noticed that using a here document (which should be equivalent to ... <<< "$var") also works.Myriapod
@Myriapod interesting. With a here document you mean <<< EOF ... EOF? Also, could you keep me informed about what they say about the bug? Or a link to it should be perfect.Dunite
Sure. Right, <<EOF$varEOF (with newlines in the expected places) works the same as the quoted here string. Bugs are reported via e-mail, so no link to a bug tracker (yet).Myriapod
let us continue this discussion in chatMyriapod
M
14

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

rrrr. Fixed several problems with IFS when it appears in the temporary environment and is used in redirections.

Although it's always a good idea to quote parameter expansions anyway, the OP's code should work as intended without quotes.


Here's an explanation of the bug. With the code

var="hello:world"
IFS=':' read var1 var2 <<< $var

the unquoted $var should be 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.

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.

In bash 4.3, as documented, the expansion of $var does not undergo word-splitting as the argument to the <<< operator; the code

var="hello:1:2 world"
IFS=: read var1 var2 <<< $var

sets var1 to hello and var2 to 1:2 world.

Myriapod answered 23/11, 2013 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.