Bash 脚本将现有数组元素附加到 space 循环拆分中的数组(whitespace?)

Bash script appending existing array element to array in loop splitting on space (whitespace?)

有人可以告诉我为什么在附加到新数组时将我的值拆分为两个数组元素吗?

这是我的代码,输出如下...

#!/bin/bash
IFS=$'\r\n' GLOBIGNORE='*' command eval  'toexec=($(cat /root/devops/commands.txt))'
#printf '%s\n' "${toexec[@]}"
dirlist=(`ls /root/devops/files/*`)
toCompress=()
toOutput=()
for fileList in ${dirlist[*]}
do
  toOutput+=($(basename ${fileList}));
  toCompress+=("${fileList}"$'\n');
done
newOut=()
printf '%s\n' "${toexec[@]}"
for (( i=0; i<${#toexec[@]}; i++ ));
do
    newval="${toexec[$i]},${toOutput[$i]}"$'\n';
    newOut+=($newval);
done
echo "======"
printf '%s\n' "${newOut[@]}"

这是输出,由 ===== 分隔,现有数组在顶部,新数组在底部。 如您所见,它将每个元素分成两个元素,在 gzipoption,filename 之间的单个 space 上拆分,例如 gzip-1,a.txt .

输出

gzip -1
gzip -9
======
gzip
-1,a.txt
gzip
-9,b.txt
...
==debug additional info==
--toexec < commands.txt--
gzip -1
gzip -9
=========================
--toCompress < ls--------
=========================
/root/devops/files/a.txt

/root/devops/files/b.txt

----toOutput-------------
a.txt
b.txt
=========================
----newOut---------------
gzip
-1,a.txt
gzip
-9,b.txt
=========================

因为值没有用双引号引起来,如

newOut+=("$newval")

这会将单个元素添加到数组。