On Fri, Jun 13, 2025 at 07:08:21 -0600, Stan Marsh wrote:
> BTW, and only tangentially related, "man bash" says that "let" and "(( ))" 
> are exactly
> the same, but "shellcheck" thinks otherwise.  "shellcheck" says you should 
> use "(( ))"
> and never use "let", but I still (mostly) use "let".

That's peculiar.  I'm not sure why shellcheck would advise using one
of them over the other, and in particular, why it would advise ((
over let.  Maybe it's because of quoting issues with let?

It's been my experience that (( looks nicer (because of the aforementioned
quoting issues), but let is *safer* in at least one case.

According to <https://mywiki.wooledge.org/BashPitfalls#pf62>:

    (( hash[$key]++ ))    # is not safe
    let 'hash[$key]++'    # is safe

It seems to be version-dependent, which the wiki page is not clear about.

(starting in bash-5.2)
hobbit:~$ unset hash
hobbit:~$ declare -A hash; key=\'\]
hobbit:~$ (( hash[$key]++ ))
hobbit:~$ declare -p hash
declare -A hash=(["']"]="1" )
hobbit:~$ bash-5.1
hobbit:~$ declare -A hash; key=\'\]
hobbit:~$ (( hash[$key]++ ))
bash-5.1: ((: hash[']]++ : bad array subscript (error token is "hash[']]++ ")
hobbit:~$ let 'hash[$key]++'
hobbit:~$ declare -p hash
declare -A hash=(["']"]="1" )

So, at least if the goal is to write code that works in multiple
versions of bash, let is definitely safer than (( in this case.

Reply via email to