在文件名中使用ls和space进行迭代

Oya*_*abi 0 arrays bash loops space

我在Ubuntu上使用bash,我在一个文件夹中有一些文件,有些文件名称中有空格,其他非文件.

我想要一个带文件名的数组.示例:[foo.txt,我是file.txt,bar.jpg等]

我的代码:

for x in "$(ls -1 test/)"; do 
    fileList+=($x)
done
Run Code Online (Sandbox Code Playgroud)

我得到:[foo.txt,我,上午,a,file.txt,bar.jpg等]

如果我把fileList+=("$x")我得到一个行数组[foo.txt我是file.txt bar.jpg等].

我该怎么做才能得到我想要的东西?

谢谢.

ens*_*nsc 5

为什么不使用shell globs?例如

for x in test/*; do
     ...
Run Code Online (Sandbox Code Playgroud)

要么

filelist=( test/* )
Run Code Online (Sandbox Code Playgroud)

编辑:

shopt -s nullglob
shopt -s dotglob
Run Code Online (Sandbox Code Playgroud)

可能也想要.