如何在 bash 中列出名称中带有白色 space 的子目录

How to list subdirectories with white space in their names in bash

我发现了一些与此类似的问题,但令人惊讶的是 none 其中对我有用。

我用脚本写了这个:

for d in $(ls -d ""); do echo $d done

$1 是我希望为其打印出子目录列表的父目录,但是,运行 这将打印出,例如,一个名为 "dir with spaces" 的目录作为单独的 3 个单词行。

您可以使用 shell globbing 而不是进程替换,这不会遇到单词扩展问题:

# to include dotfiles and not iterate empty directory
shopt -s dotglob nullglob

for d in ""/*; do
    echo "$d"
done

或者您可以使用非常常见的 find ... -print0 | xargs -0 ... 模式。