我确定我错过了一些东西,但我无法弄清楚。鉴于:
$ find -type f
./hello.txt
./wow.txt
./yay.txt
接下来的两个命令如何呈现不同的结果?
$ find -type f -exec basename {} \;
hello.txt
wow.txt
yay.txt
$ find -type f -exec echo $(basename {}) \;
./hello.txt
./wow.txt
./yay.txt
我确定我错过了一些东西,但我无法弄清楚。鉴于:
$ find -type f
./hello.txt
./wow.txt
./yay.txt
接下来的两个命令如何呈现不同的结果?
$ find -type f -exec basename {} \;
hello.txt
wow.txt
yay.txt
$ find -type f -exec echo $(basename {}) \;
./hello.txt
./wow.txt
./yay.txt
上,使用bash -x
调试器展示了这种快速调试,
[的例子是我自己的,只是为了演示的目的]
bash -xc 'find -type f -name "*.sh" -exec echo $(basename {}) \;'
++ basename '{}'
+ find -type f -name '*.sh' -exec echo '{}' ';'
./1.sh
./abcd/another_file_1_not_ok.sh
./abcd/another_file_2_not_ok.sh
./abcd/another_file_3_not_ok.sh
而对于刚刚basename {}
bash -xc 'find -type f -name "*.sh" -exec basename {} \;'
+ find -type f -name '*.sh' -exec basename '{}' ';'
1.sh
another_file_1_not_ok.sh
another_file_2_not_ok.sh
another_file_3_not_ok.sh
正如你可以在第一个例子中看到,echo $(basename {})
分两步得到解决,basename {}
不过是basename
实际文件(输出纯文本文件名),然后将其解释为echo {}
。所以它只是模仿-ING的确切行为当您使用find
与exec
和echo
文件作为
bash -xc 'find -type f -name "*.sh" -exec echo {} \;'
+ find -type f -name '*.sh' -exec echo '{}' ';'
./1.sh
./abcd/another_file_1_not_ok.sh
./abcd/another_file_2_not_ok.sh
./abcd/another_file_3_not_ok.sh
$(基名{})在命令运行前评估。结果是{},因此命令echo $(basename {})变为echo {},并且不会为每个文件运行基本名称。