有没有办法将选项作为数组传递以在 bash 中查找命令?

Is there a way to pass options as an array to find command in bash?

rsync 中,我可以在数组中定义 exclusions/options 并在后续命令中使用这些数组,例如。 G。

rsync "${rsyncOptions[@]}" "${rsyncExclusions[@]}" src dest

我想使用 find 命令实现同样的效果,但我找不到让它工作的方法:

findExclusions=(
    "-not \( -name '#recycle' -prune \)"
    "-not \( -name '#snapshot' -prune \)"
    "-not \( -name '@eaDir' -prune \)"
    "-not \( -name '.TemporaryItems' -prune \)"
)
LC_ALL=C /bin/find src -mindepth 1 "${findExclusions[@]}" -print

无论我尝试以何种组合定义数组(单引号与双引号、转义括号),我总是以错误告终:

find: unknown predicate `-not \( -name '#recycle' -prune \)'
# or
find: paths must precede expression: ! \( -name '#recycle' -prune \)

正确的做法是什么?

这里的问题是 "-not \( -name '#recycle' -prune \)" 不是一个参数,而是 6 个参数。你可以这样写:

findExclusions=(
    -not '(' -name '#recycle' -prune ')'
    -not '(' -name '#snapshot' -prune ')'
)