I'm not absolutely positive, but I'm sure it's primarily to remove ambiguity.
In shell scripts, you'll notice the "natural" format for data is a string/text. It makes sense because commands are text and a vast number of files are text (configuration, data, etc.). So, if you were to say:
Code:
document_number=document_number+1
document_number=${document_number}+1
For the first, are you trying to assign the string "document_number+1" to a variable or are you trying to add 1 to a variable? If you're trying to add 1, then it'd probably make sense to reference the contents of the variable, right? So that brings us to the second statement. However, since a bash script has no specific data types, it can't know if the contents of document_number is a string or an actual number. Besides, having a string with "
something+1" might be a legitimate argument to pass for a program. It would also be a potential error for it to
convert the contents to an integer as a convenience. There may be some legitimate use for a string of the form "59test+1". So, converting "59test" to 59 to perform the addition could potentially derail the purpose of the script completely.
So, the solution the bash maintainers picked was to force the user to explicitly declare when you want to do math. You can do it with the nested parentheses like I did, or with the "let" keyword, like so:
Code:
((document_number=document_number+1))
let document_number=document_number+1
They're both equivalent. I use the parentheses for a couple reasons. It's easy two just double-punch '(' than type "let " (ultra laziness I admit), emacs does parenthesis matching making sure my expression is as expected, and I think it sets off the code a little more making it obvious this is a numeric calculation rather than a textual operation.