【问题标题】:Does * match hyphens in a glob in bash?* 是否匹配 bash 中的 glob 中的连字符?
【发布时间】:2021-08-11 12:49:55
【问题描述】:

今天我在一个目录及其子目录中寻找一个名为 cfg-local.properties 的文件。

我用过:

find . -iname *.properties

输出:

./gradle.properties

然后,我尝试了:

find . -iname *-*.properties

输出:

./cfg/src/test/resources/cfg-local.properties
./cfg/src/main/resources/cfg-local.properties
./cfg/build/classes/test/cfg-local.properties
./cfg/build/resources/test/cfg-local.properties
./cfg/build/resources/main/cfg-local.properties

星号不应该与 glob 表达式中的任何字符(包括连字符)匹配吗?

【问题讨论】:

    标签: bash glob


    【解决方案1】:

    是的,它会匹配,但是您实际上正在运行命令:

    find . -iname gradle.properties
    

    安装,运行:

    find . -iname '*.properties'
    

    当您键入并运行 find . -iname *.properties 时,*.properties 部分将被 shell 扩展以匹配文件。 (称为 globbing 的过程)。

    在第二个示例中,find . -iname *-*.properties*-*.properties 不匹配任何文件,因此不执行文件名扩展,*-*.properties 逐字传递给 find 命令。

    将单词括在单引号中,例如 '*.properties' 将阻止 shell 执行通配符。

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 2015-11-18
      相关资源
      最近更新 更多