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
- Linux Shell - Trying to split a string into two variables
- GAC was split into two.
- use regexp() to split a string
- Java – How to split a string
- How to split a string in C++
- P16 (**)-P17 (*) Drop every Nth element from a list;Split a list into two parts.
- Split String into Valid Words
- bash split string into array
- Powershell - how to split string by a string
- How to split a string to array in objective-c?
- P17 (*) Split a list into two parts; the length of the first part is given.
- C#: Problem trying to resolve a class when two namespaces are similar
- Sending variables into a SWF file
- ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers.
- Split array into two equal sum subArray
- How to group anagrams in a string into an array
- A SQL Function which used to split string by char
- Split string to palindrome
- pentaho report designer 5.01报表分组
- 浏览器中F5与Ctrl+F5的区别(有图有真相)
- OpenSearch description document
- iOS开发之Objective-C与JavaScript的交互
- 修改eclipse字符集
- Linux Shell - Trying to split a string into two variables
- TCP三次握手/四次挥手详解
- 灰度图像阈值化分割常见方法总结及VC实现
- grep在Linux下的神奇定位功能 ----------------- 正则表达式
- 内存管理:高质量C++/C编程指南
- 使用OpenSearch description documents自定义您的搜索引擎并添加到IE浏览器中
- iOS设计模式之观察者模式
- scope
- 在view上添加一条细线