As clearly spelled out in the Bash documentation on shell arithmetic, if you prefix a number with a
0 within an arithmetic expansion expression (e.g.,
$(( expr ))), it's treated as octal. Similarly, you can use
b#n for a number
n in base
However, irrespective of the base you use for the arithmetic inside the expression, the value is always returned as base 10. How do I return octal from the result of an arithmetic expansion?
My original use case for this was to warn a user that the permissions on their
~/.my.cnf were too lax (group- or world-readable) by ANDing together the permission bits of the file with a bitmask, but I later decided that testing the output of
find ~/.my.cnf -perm +0044 is probably cleaner, and more portable, since
stat format strings are not standard between macOS/BSD and Linux. Yet, the question remains.