this post was submitted on 05 Mar 2025
193 points (99.5% liked)

Programmer Humor

34449 readers
204 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 12 points 2 weeks ago (1 children)

I gave up Bash scripting. I explicitly do "shell scripting" now, where "shell" is implied to be a POSIX compliant shell of any type.

[–] [email protected] 7 points 2 weeks ago (1 children)

But, but like ... hear me out.

echo $((1+1))

[–] [email protected] 1 points 1 week ago (1 children)

That is a posixly correct method to do arithmetic expressions.

[–] [email protected] 1 points 1 week ago (1 children)

Huh, seems you're right. I was under the impression this wouldn't work in dash but apparently that's wrong.

[–] [email protected] 1 points 1 week ago

Yep! The functionality for performing arithmetic expressions this way is called “arithmetic expansion”.

2.6.4 Arithmetic Expansion

Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for arithmetic expansion shall be as follows:

$((expression))

The expression shall be treated as if it were in double-quotes, except that a double-quote inside the expression is not treated specially. The shell shall expand all tokens in the expression for parameter expansion, command substitution, and quote removal.

Next, the shell shall treat this as an arithmetic expression and substitute the value of the expression. […]

https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_04