koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.45k stars 1.78k forks source link

SC2004: corner case in arithmetic expressions #2912

Open nsoggia opened 9 months ago

nsoggia commented 9 months ago

Shellcheck should not ask to remove $/${}'s right after ~'s in arithmetic expressions.

I just found this problem here: https://github.com/linux-pam/linux-pam/commit/b6011a23d51acff6eb492cb33aa5c2582da6fab#commitcomment-137982622, where Shellcheck suggested SC2004 (style): $/${} is unnecessary on arithmetic variables.

$((0777 & ~$mask)) was changed to $((0777 & ~mask)), but the two expressions are not always equivalent! 0777 and not "variable named mask" is not the same as 0777 and not "home directory of user named mask".

This is a subtle corner case. As I said in the original comment, if the variable "mask" is not empty, then there will be no problems, but if the variable does not exist or it is empty, then bash will pick up the home directory of a system user named as the variable (if it exists, of course). Cheers