注意:通常建议使用 (( ... ))
而不是 let
。 let
会受到分词和全局扩展的影响,这是算术评估中很少需要的。来源: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*c
与bcc
不匹配。例如,名为 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/