bash - 当与 let 结构一起使用时,间距如何影响通配符运算符?

标签 bash glob let arithmetic-expressions

注意:通常建议使用 (( ... )) 而不是 letlet 会受到分词和全局扩展的影响,这是算术评估中很少需要的。来源:link link .

无论如何,我很想更好地理解通配符在以下 bash let 结构中的工作原理:

$ set -o xtrace  # used to figure out what is happening


# This section shows that globbing is performed
$ b=2
$ c=4

# watch out: '*' is the globbing operator here, so * expands 
# to the files in the current directory 
$ let a=b * c # This line will expand to: 
              #     let a=b bcc declaration.sh let-basics.sh c
              # Only 'b' will be evaluated because the remainder
              # after 'b' causes a syntax error.
$ echo "${a}" 
2             # Only b was evaluated so 2 is expected


# In contrast, it seems as if no globbing happens 
# here, even though there is a match on the 'bcc' file 
# in this directory.
$ let a=b*c
$ echo "${a}" 
8

为什么 let a=b*c 中的 * 不被计算为通配符?


我使用了以下 bash 版本:

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

最佳答案

how globbing works

文件名扩展将作为模式单词替换为与该模式匹配的文件列表。您可以在 bash manual 中了解它。另请参阅man 7 glob .

within the following bash let-constructs:

就像任何其他命令一样。 let 无论如何都没有什么特别的。

# here no globbing happens, even though there is a match on 
# the 'bcc' file

我不明白如何将 a=b*c 替换为名为 bcc 的文件。 a= 发生了什么? = 是一个普通字符。 a=b*cbcc 不匹配。例如,名为 a=bcc 的文件可以匹配 a=b*c

Why isn't the * evaluated in let a=b*c as the globbing operator?

是的,您只是没有与该 glob 匹配的文件。

关于bash - 当与 let 结构一起使用时,间距如何影响通配符运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68721985/

相关文章:

python - 如何创建一个 python 脚本,以便在目录中的 csv 文件在过去 24 小时内未更新时发送电子邮件?

python - Python 中符合 POSIX 标准的 glob

javascript - 'let' 是否覆盖全局声明并抛出 ReferenceError?

clojure - 在 Clojure 中返回表达式序列中非最后一个表达式的值的惯用方法

php - 有没有办法只使用 glob() 文件?

lisp - Ocaml 等同于 Lisp 的 let*?

bash - 使用 mv 删除文件名的一部分

bash - Zsh 中有没有类似 .bash_profile 的东西?

bash - 从 docker 运行更长的命令

linux - 如何保留 zip 压缩后原始文件的时间戳?