我正在尝试使用除空格以外的分隔符将字符串拆分为数组,以保留空格。
例如,如果我运行:
files=$(echo "foo.txt:bar.txt:my story.txt" | tr ":" "\n")
for f in $files; do
echo $f
done
我得到:
foo.txt
bar.txt
my
story.txt
如何保存空间?
files=$(cmd)
不是数组分配。这是一个字符串分配(的输出cmd
分配给一个常规变量files
)。
我建议使用read -a
拆分字符串,因为glob不会成为问题:
IFS=: read -ra files <<< "foo.txt:bar.txt:my story.txt"
for f in "${files[@]}"; do
echo "$f"
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句