Bash-c参数传递到find

Bash-c参数传递到find,bash,find,arguments,Bash,Find,Arguments,这个命令有效 find . -name \*.txt -print bash -c 'echo . $0 $1 -print' "-name" "\*.txt" 并输出两个文件名 这个命令有效 find . -name \*.txt -print bash -c 'echo . $0 $1 -print' "-name" "\*.txt" 并输出此结果: -名称*.txt-打印 但是这个命令 bash -c 'find . $0 $1 -print' "-name" "\*.txt"

这个命令有效

find . -name \*.txt -print
bash -c 'echo . $0 $1 -print' "-name" "\*.txt"
并输出两个文件名

这个命令有效

find . -name \*.txt -print
bash -c 'echo . $0 $1 -print' "-name" "\*.txt"
并输出此结果:

-名称*.txt-打印

但是这个命令

bash -c 'find . $0 $1 -print' "-name" "\*.txt"
不会给出错误,但也不会输出任何内容


有人能告诉我这里发生了什么吗?

看起来您正试图使用
“\*.txt”
阻止全局扩展,以便
find
命令可以看到
*.txt
,而不是
foo.txt

然而,它最终看到的是
\*.txt
。没有与该模式匹配的文件,因此您看不到任何输出

要使
find
参见
*.txt
作为其第三个参数,可以执行以下操作:

bash -c 'find . $0 "$1" -print' "-name" "*.txt"

编辑:你真的得到了
-name*.txt-打印
,作为第一个命令的输出,在该命令中将
find
替换为
echo
?当我运行该命令时,我得到
-name\*.txt-打印

弗朗西斯科的建议。但我仍然对这里的行为感到困惑。 我们知道,在find命令中放置不带引号的通配符通常会导致错误。也就是说:

find . -name *.txt -print
查找:路径必须位于表达式:
HowTo Word Split.txt“查找:
谓词
-name'后可能出现的不带引号的模式

但是,将通配符放在单引号中(如果只有1个字符,则转义通配符)的工作方式如下:

find . -name \*.txt -print
它给出了这个输出(在两个单独的行上)

所以在bash-c版本中,我的想法是:

bash -c 'find . $0 $1 -print' "-name" "*.txt"
将导致*.txt在传入cmd字符串之前就已展开, 使用单引号将导致尝试执行(在arg替换和-c生效后)

正如我刚才演示的,这是行不通的

然而,-c开关似乎有某种魔力,如在bash提示符下设置-x所示,如下所示:

$ set -x
$ bash -c ' find . $0 "$1" -print' "-name" "*.txt"

+ bash -c ' find . $0 "$1" -print' -name '*.txt'
./HowTo-Word-Split.txt
./bash-parms.txt
请注意,即使我在-c行中使用了双引号,bash实际上还是使用单引号在参数周围执行find,从而使find工作


问题解决了。:

您确定有
*.txt
文件要打印吗?
找到了什么-name\*.txt-print
print?所以我看到的问题是,使用bash-c为bash处理器提供了两个扩展
*.txt
的机会。我不确定修复方法是什么,但请看一看:
echo
输出中的差异可能来自尝试解释转义本身的
echo
。不同版本的echo在这类事情上完全不一致。我错了。echo cmd实际上输出*.txt