我有字符串“hi how are you”,我想将此字符串放入数组中,如下所示。但我想保留空间。关于如何做到这一点的任何想法?
a[0] a[1] a[2] 3 4 5 6 .... should have
h i <space> h o w <space> .... and so on.
一种方法,肯定会有更好的解决方案,但这似乎对我有用:
unset arr; IFS=; for c in $(sed 's/./&\n/g' <<<"hi how are you"); do arr+=("$c"); done; echo "${arr[@]}"
它产生:
h
i
h
o
w
a
r
e
y
o
u
eval a=( $(echo "hi how are you" | sed "s/\(.\)/'\1' /g") )
eval
真的很难看,也许有人可以在没有...的情况下想出一些东西
可能不快,但避免需要sed
:
z=()
while read -n 1 x; do
z+=( "$x" )
done <<<"hi how are you"