I am trying to get my head around bash but am struggling with some core foundations.
I can see an arithmetic statement is defined by double parentheses
((5-3))
but if I try to echo
that or us it in shift
I get an error
echo ((5-3))
-bash: syntax error near unexpected token `('
In examples it shows that prefixing it with a dollar symbol will make it work
echo $((5-3))
Why is that? The syntax looks as though I'm applying the arithmetic to a blank variable and then onto the echo
function. Why can't I just pass the result of the arithmetic into the echo
function? How do I know when to prefix an arithmetic statement with a dollar symbol in other instances.
Please excuse my ignorance. Im so used to web development languages and Bash is just so different.
((...))
is an arithmetic expression.
$((...))
is an arithmetic expansion.
The difference is that an expression is a unit on its own but an expansion just gets replaced with a value.
This is the same thing as the difference between.
$ echo foo
foo
and
$ $(echo foo)
-bash: foo: command not found
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With