在bash中,我怎么转换空间分隔的字符串到一个数组? [英] In bash, how do I convert a space-delimited string into an array?
本文介绍了在bash中,我怎么转换空间分隔的字符串到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一个空格分隔字符串的变量,
行=1 1.50字符串
我想该字符串空间拆分作为分隔符,结果存储在一个数组,所以以下内容:
回声$ {ARR [0]}
回声$ {常用3 [1]}
回声$ {常用3 [2]}
输出
1
1.50
串
带我找到了一个解决方案,它不工作:
ARR = $(回声$ {}行)
如果我上面运行在此之后的echo语句,我得到:
1 1.50字符串
[空行]
[空行]
我也试过
IFS =
ARR = $(回声$ {}行)
具有相同的结果。有人可以帮助,好吗?
解决方案
ARR =($行)
或
阅读-a改编<<< $线
I have a variable which contains a space-delimited string,
line="1 1.50 string"
I want to split that string with space as a delimiter and store the result in an array, so that the following:
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
outputs
1
1.50
string
Somewhere I found a solution which doesn't work:
arr=$(echo ${line})
If I run the echo statements above after this, I get:
1 1.50 string
[empty line]
[empty line]
I also tried
IFS=" "
arr=$(echo ${line})
with the same result. Can someone help, please?
解决方案
arr=($line)
or
read -a arr <<<$line
这篇关于在bash中,我怎么转换空间分隔的字符串到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文