Bash find-exec basename{}vs find-exec echo$(basename{})
我肯定我错过了什么,但我想不出来。鉴于:Bash find-exec basename{}vs find-exec echo$(basename{}),bash,Bash,我肯定我错过了什么,但我想不出来。鉴于: $ 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调试器对其进行的快速调试演
$ 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
调试器对其进行的快速调试演示了这一点
[示例是我自己的,仅供演示]
只需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{}
。因此,当您将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
$(basename{})在命令运行之前进行计算。结果是{},因此echo$(basename{})命令变为echo{},并且不会为每个文件运行basename
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