如何使用变量作为查找命令的路径

How to use variable as a path for find command

谁能给我一个提示,我如何才能正确制作以下 bash 脚本 运行:

path="/path/"
excludepath="! -path \"/path/to/exclude/*\" "

find "$path" "$excludepath" -name *."txt" -type f

当我尝试 运行 时,我得到:

find: `! -path "/path/to/exclude/*"': No such file or directory

提前致谢。

您的问题是 "$excludepath" 是单个参数,即使它包含空格。要放置多个参数,请使用字符串数组而不是字符串:

excludepath=(! -path "*webshow*")
find "$path" "${excludepath[@]}" -name "*.txt" -type f

不过,这是一个 bashism(偶然发现这个但不使用 bash 的人需要做其他事情)。