bash - bash 中无法理解文件名扩展

标签 bash shell glob

我有以下代码:

#!/bin/sh

LOG="myfile.log"

ls -l "${LOG}_*"

find . -type f -name "${LOG}_*" -exec ls -l {} \;

产生以下输出:

$ ./test.sh

ls: cannot access myfile.log_*: No such file or directory

-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1928
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1926
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1930
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1927
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1929

我不明白为什么“find”命令有效,而“ls”命令无效

干杯

最佳答案

Shell 仅在未加引号时展开星号。

Find 在 -name 本身中扩展通配符,并且您通常必须引用它们以防止 shell 扩展它们。

关于bash - bash 中无法理解文件名扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802338/

相关文章:

bash - 如何使 grep 不解释搜索字符串中的特殊字符?

linux - shell脚本中如何知道后台作业是否完成?

bash - 为什么我的脚本在命令后突然退出?

regex - 无法正确解析文件;全局不工作

iphone - 从 iPhone 摄像头创建直播并将其托管在网页上

bash - 当您执行 Ctrl-C 时 BASH 中会发生什么(提示,它不仅仅是发送 SIGINT)

regex - glob 样式模式和正则表达式之间有什么区别?

regex - 我可以在 mac 终端中将正则表达式与 cd 命令一起使用吗?

扫描网络的Linux脚本

linux - 在同一子目录中查找具有相同 MD5 的文件