Bash 如何在变量中使用双引号在反引号中执行它?

Bash 如何在变量中使用双引号在反引号中执行它?,bash,Bash,我想把find命令的结果放到变量中,但我在命令替换块中遇到了变量问题。下面是一个简短的例子: #!/bin/bash pattern='"file*"' res=$(find . -maxdepth 1 -type f -name ${pattern}) # doesn't work #res=$(find . -maxdepth 1 -type f -name "file*") # works echo $res 怎么了?试试看 pattern="file*" res=$( find .

我想把find命令的结果放到变量中,但我在命令替换块中遇到了变量问题。下面是一个简短的例子:

#!/bin/bash

pattern='"file*"'
res=$(find . -maxdepth 1 -type f -name ${pattern}) # doesn't work
#res=$(find . -maxdepth 1 -type f -name "file*") # works
echo $res
怎么了?

试试看

pattern="file*"
res=$( find . -maxdepth 1 -type f -name "${pattern}" )
这等于“find.-maxdepth 1-type f-name file*”,当有多个文件具有这样的名称掩码时,它将不起作用。例如“文件1”和“文件2”。