Solved Why isn't bash math working?

Solved issue

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
790
Reaction score
644
Credits
5,031
I've been reading this https://phoenixnap.com/kb/bash-math + other articles which are almost identical to this one and whichever example I try - it doesn't work. It either returns errors or completely wrong results. For instance, in the article
Code:
echo $((2+3))
returns 5, but when I try it, it returns "invalid command" or something like that.

I have a GUI calculator but it's very inconvenient for the things I need to calculate, that's why I'm trying to do math in bash. If I can figure it out how it works, I could write a script to make things even easier than the GUI calculator.
 


If you did something like this:
Bash:
echo $(2+3)
You will get the error message bash: 2+3 command not found.
Because $() is a process substitution - so that acts as a placeholder for the output from a command.
So bash thinks that 2+3 is the name of a command.

But if you type the following into a terminal:
Bash:
echo $((2+3))

You should see the number 5 output to the screen.
$(()) is the syntax for mathematical substitutions in bash.
So you put your calculation inside the double brackets and the result is substituted into your echo command.

It's worth noting that bash can only deal with integer mathematics (whole numbers). It can't deal with floating point/real numbers. So if you need to use precise, floating point calculations, you should either take a look at bc (a terminal based, arbitrary precision calculator), or consider using a scripting language that can deal with floating point calculations like perl, python or ruby.

I've just taken a look at the link you posted and everything on that page works just fine for me.
 
Bash:
echo $((2+3))
If I copy-paste that, it works. But if I type it manually, it doesn't and these are the errors I get when I try to use it with my expressions:

Code:
[rado@arch]: ~>$ echo $((3000/1.777777))
bash: 3000/1.777777: syntax error: wrong arithmetic operator (wrong token is '.777777')
[rado@arch]: ~>$ echo $((3000/1.777777))
777777

So far the only thing that worked properly is

Code:
echo "math expression" | bc

which I just turned into a script which will be expanded over time.

Code:
echo "$1/1.777777" | bc

But for now I need to add measurement units at the end of the result and on the same line as the result.
 
If I copy-paste that, it works. But if I type it manually, it doesn't and these are the errors I get when I try to use it with my expressions:
Try running it under a different user account, maybe there is something in your shell configuration that prevents something from happening?
 
Try running it under a different user account, maybe there is something in your shell configuration that prevents something from happening?
It wasn't anything to do with the shell configuration for the user.
The problem was the fact that @rado84 was using floating-point numbers in his calculations. And as I mentioned in my post, bash cannot deal with real/floating point calculations. Bash can only deal with integer mathematics NOT floating point. Hence the need for @rado84 to use bc.
 
It wasn't anything to do with the shell configuration for the user.
The problem was the fact that @rado84 was using floating-point numbers in his calculations. And as I mentioned in my post, bash cannot deal with real/floating point calculations.
In @rado84 OP he was doing 3+2 and got the same error.
Code:
echo $((2+3))
returns 5, but when I try it, it returns "invalid command" or something like that.
That's why I mentioned it. I didn't see that he got it working a reply letter by typing it manually. So something in copy pasting must have not gone right.
Bash can only deal with integer mathematics NOT floating point.
Yes I know that.
Code:
maarten@masterzen:~$ echo $SHELL
/bin/bash
maarten@masterzen:~$  echo $((3000/1.777777))
bash: 3000/1.777777: syntax error: invalid arithmetic operator (error token is ".777777")
maarten@masterzen:~$
Other option is to switch to zsh, since zsh does seem to support it.
Code:
echo $SHELL
/bin/zsh

echo $((3000/1.777777))
1687.5007382815731
 
something in copy pasting must have not gone right.
You didn't get what I wrote earlier. I said that the echo math WORKS when I copy-paste it FROM the article. But if I type it manually, it doesn't and returns the errors I put in the code tag.

I lived with bash for 9 years, I don't wanna start learning a new shell, just for a math function.
"| bc" seems to be working quite fine, so no need of a new shell.
 
You didn't get what I wrote earlier. I said that the echo math WORKS when I copy-paste it FROM the article. But if I type it manually, it doesn't and returns the errors I put in the code tag.
I said it the other way around, almost the same but in reverse. ;)

I lived with bash for 9 years, I don't wanna start learning a new shell, just for a math function.
Anything you can run in bash you can run in zsh, so not much learning to do.
 
Last edited:
if you have ZSH installed you can simply open the terminal type in zsh and you are in a temporary zsh shell
after you are done just type in bash and you are back in bash
2.png

3.png
 
In @rado84 OP he was doing 3+2 and got the same error.

That's why I mentioned it. I didn't see that he got it working a reply letter by typing it manually. So something in copy pasting must have not gone right.
I get the feeling that @rado84 copy/pasted the echo $((3+2)) into the shell.
Then he tried manually entering echo $((3000/1.777777)) and was perplexed when he got errors.

I don't see any way that the expression echo $((3+2)) would only work when copy/pasted, and NOT when it was manually typed. That makes no sense.

The problem was purely down to the use of floating point values in bash.
And once again - bash can only deal with integer maths. If you need to use floating point/decimal/real numbers in bash, you need to use an external tool like bc, or another interpreter (python/ruby/perl etc).

I forgot that zsh could do floating point maths. Haven't used it in donkeys years.
The only reason I stick with bash so doggedly is because it's guaranteed to be pre-installed by default, pretty much everywhere. And because I'm supremely lazy! Ha ha.
 
if you have ZSH installed you can simply open the terminal type in zsh and you are in a temporary zsh shell
after you are done just type in bash and you are back in bash
View attachment 19609
View attachment 19610
I have a minor correction.
If you're in bash and you start zsh, the way to get back to bash is to enter the exit command.
That will exit zsh and will return you to the bash shell you invoked zsh from.
 
I often used AWK for calculations

echo 4 9 | awk ' { printf $1 * $2 } '

The good thing is that you can do more complex things, like this:

echo 4 9 80 | awk ' { printf $1 * $2 + $3 } '

annd you can just add text where you want (as what printf is used for obviously)
 

Staff online


Latest posts

Top