When it comes to Arithmetic in bash scripts it would seem that doing some basic operations can be done, however when it comes to doing anything a little advanced it might be best to wrap another programing environment.
Still some basic Arithmetic is possible with bash alone, and in this post I will be going over some basic examples of how to preform some basic addition, and subtraction operations. I could get into some more complex expressions, but doing so often might require wrapping a more advanced programing environment and when doing that it might be best to just work out what I need to do as a program written in that environment.
There are two ways that I find myself going about doing basic arithmetic in bash, one of which is the use of Arithmetic expansion. This I have found is the most robust form of doing basic math operations in bash, however there are a few other ways to create basic expressions.
In this section I will just be going over some very basic examples of arithmetic using the various methods of doing so that I am familiar with.
Arithmetic Expansion is thus far the way that I often go about creating basic expressions in bash. There are several forms of what us referred to as parameter expansion in the bash man page. It is a good idea to become familiar with the various other forms of expansion that are there to work with, but that might be a matter for another post.
This kind of expansion works by typing a dollar sign followed by two sets of parentheses. Inside this set of double parentheses I write the expression that I want to have evaluated to a result. The result I can then save to a variable to be used with a command like echo.
So if I save the above bash script example as something like arith_exp.sh, and then make it executable with chmod I can then call the script directly.
I then get the expected result in the standard output.
Another way to create basic expressions would be to use the let command. This might be a cleaner way of going about creating expressions as I do like the readability of this better.
There might be a few other commands and tricks when it comes to basic expressions using just bash. However there are limits to what can be done when it comes to working something out that is a little advanced. When it comes to working out a complex expression there is a certain line where once crossed the only way to get it done is to just switch to another language that is a little more capable.
Bash has its limits when it comes to working out expressions. If I do need to do anything that is a little complex bash will just not cut it. However when it comes to bash scripts I do have a whole world of Linux commands to work with, and one such command is of course nodejs.
So then this is one way to go about doing some fairly complex expressions if I can just work out what the javaScritp code should be. However there is one draw back and that is that the results will happen real slow this way.
When working out bash scripts thus far the math involved is typically not that advanced. The bash arithmetic expansion syntax works okay when it comes to things like stepping a variable in a loop, and other basic tasks that is called for in a bash script.