如何将 find 的输出分配给数组

aga*_*era 5 linux bash shell

在 linux shell 脚本中,我尝试将 find 的输出设置为数组,如下所示

#!/bin/bash
arr=($(查找.-type-f))

但它会给出错误,因为 -type 应该只包含一个字符。谁能告诉我问题出在哪里。

谢谢

che*_*ner 6

如果您使用bash4,则该readarray命令可以与进程替换一起使用。

readarray -t arr < <(find . -type f)
Run Code Online (Sandbox Code Playgroud)

正确支持所有文件名,包括包含换行符的文件名,需要更多的工作,以及find支持的版本-print0

while read -d '' -r; do
    arr+=( "$REPLY" )
done < <(find . -type f -print0)
Run Code Online (Sandbox Code Playgroud)