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)?
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=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With