Linux Shell - Trying to split a string into two variables

来源:互联网 发布:电视机机顶盒软件下载 编辑:程序博客网 时间:2024/04/30 05:37

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

Thanks in advance!
 

 

Solution:

 

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=

0 0
原创粉丝点击