Note: Generally it is advised to use (( ... ))
over let
. let
is subject to word splitting and glob expansion, which is something you rarely want in arithmetic evaluation. Sources: link link.
Regardless, I would love to better understand how globbing works within the following bash let-constructs:
$ 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
Why isn't the *
evaluated in let a=b*c
as the globbing operator?
I used the following bash version:
$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)