Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个字符串
echo $STRING
这使
first second third fourth fifth
基本上是一个列表分隔的空格。
我如何获取该字符串并使其成为一个数组,以便
array[0] = first array[1] = second
ETC..
我试过了
IFS=' ' read -a list <<< $STRING
但是当我做一个
echo ${list[@]}
它只打印出“第一”,没有别的
其实很简单:
list=( $STRING )
或者更详细地说:
declare -a list=( $STRING )
PS:您不能导出 IFS 并在同一命令中使用新值。您必须先声明它,然后在以下命令中使用它的效果:
$ list=( first second third ) $ IFS=":" echo "${list[*]}" first second third $ IFS=":" ; echo "${list[*]}" first:second:third