The difference is that (( is a “compound command”, similar to [[ (evaluate conditional expression), while $(( )) is “aritmetic expansion”. They behave in almost exactly the same way but are used in different contexts - the former uses “exit codes” while the latter returns a string, so the former would be used where you would expect a command, while the latter would be used where you expect an expression.
This is similar to how there is ( compound command (run in a subshell), and $( ) (command substitution). You can actually use the former to define a function too (as it’s a compound command):
real_exit() { exit 1; }
fake_exit() ( exit 1 )
Calling real_exit will exit from the shell, while calling fake_exit will do nothing as the exit 1 command is executed in a separate subshell. Notice how you can also do the same in a command substition (because it runs in a subshell):
echo $(echo foo; exit 1)
Will run successfully and output foo.
It is another one of those unknown, very rarely useful features of bash.
Oh I see it, but for some reason I was taught to always use
$(( arith ))instead of(( arith ))and I guess I’m just wondering what the difference isThe difference is that
((is a “compound command”, similar to[[(evaluate conditional expression), while$(( ))is “aritmetic expansion”. They behave in almost exactly the same way but are used in different contexts - the former uses “exit codes” while the latter returns a string, so the former would be used where you would expect a command, while the latter would be used where you expect an expression.This is similar to how there is
(compound command (run in a subshell), and$( )(command substitution). You can actually use the former to define a function too (as it’s a compound command):real_exit() { exit 1; } fake_exit() ( exit 1 )Calling
real_exitwill exit from the shell, while callingfake_exitwill do nothing as theexit 1command is executed in a separate subshell. Notice how you can also do the same in a command substition (because it runs in a subshell):echo $(echo foo; exit 1)Will run successfully and output
foo.It is another one of those unknown, very rarely useful features of bash.
amazing, thanks!