How can I do basic maths in bash?

2020-03-26 10:43发布

I've made something that I think ought to be a shell script called bashispainful.sh:

Code:

#!/bin/bash
Var1= $((10*5))
Var2= $((10/5))
Var3= $((10-5))
Var4= $((10+5))
Var5= ($(Var4)*$(Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5

I would like to receive the output:

50
2
5
15
75

My current output:

./Bashispainful.sh: line 2: 50: command not found
./Bashispainful.sh: line 3: 2: command not found
./Bashispainful.sh: line 4: 5: command not found
./Bashispainful.sh: line 5: 15: command not found
./Bashispainful.sh: line 6: syntax error near unexpected token `('
./Bashispainful.sh: line 6: `Var5= ($(Var4)*$(Var3))'

My aim here is to get any kind of grip on being able to do basic maths in bash. I have tried every website and every combination of brackets, dollar signs and lack thereof that I can think of and frankly I am baffled and desparate. How can I perform simple maths in bash?

(Please make this easy to understand - please assume that I am a non-programmer and stupid)

标签: linux bash shell
4条回答
做个烂人
2楼-- · 2020-03-26 11:00

You can't put spaces before or after the =, or bash tries to execute the command $((10*5)) (i.e., 50) while setting Var1 to the empty string in a local environment. Also, using single parentheses without a dollar sign (…) invokes a subshell, with a dollar sign $(…) invokes a subshell and is substituted with the output of a command, and the dollar sign is optional inside an arithmetic expression.

So what you want to do is:

#!/bin/bash
Var1=$((10*5))
Var2=$((10/5))
Var3=$((10-5))
Var4=$((10+5))
Var5=$((Var4*Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5
查看更多
虎瘦雄心在
3楼-- · 2020-03-26 11:01

Remove the space before the $ and move the bracket from the line in the Var5 declaration.

#!/bin/bash
Var1=$((10*5))
Var2=$((10/5))
Var3=$((10-5))
Var4=$((10+5))
Var5=$((Var4*Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5

The space screws with the declarations, and you want to read the value from Var4 and Var3, so you need two brackets.

查看更多
叛逆
4楼-- · 2020-03-26 11:08

Bash is not painful:

#!/bin/bash
Var1=$((10*5))
Var2=$((10/5))
Var3=$((10-5))
Var4=$((10+5))
Var5=$((Var4*Var3))
echo $Var1
echo $Var2
echo $Var3
echo $Var4
echo $Var5

The output is:

50
2
5
15
75

Notes

Consider:

Var1= $((10*5))

After bash does arithmetic substitution, that command looks like:

Var1= 50

That command says (a) temporarily set Var1 to empty, and (b) run the command named 50. Since there is no command named 50, you receive the error:

Bashispainful.sh: line 2: 50: command not found

The solution is remove the space so that the Var1 is assigned to a non empty value:

Var1=$((10*5))

Consider:

Var5= ($(Var4)*$(Var3))

This sets Var5 temporarily to empty for a command whose name starts with (. That is not valid. Hence the error:

./Bashispainful.sh: line 6: syntax error near unexpected 

The correct bash syntax for arithmetic is $((...)). Thus, that line could be written:

Var5=$(($Var4*$Var3))

However, because bash is not painful, it understands that strings inside arithmetic expressions are shell variables. Thus, the dollar signs are unnecessary and the above can be simplified to:

Var5=$((Var4*Var3))

Alternatively, since the above is just doing assignment to variable, it can be further simplified to:

((Var5=Var4*Var3))

Note that $((...)) returns the value of the arithmetic computation but ((...)) does not return any value. It does set an exit code which makes it useful for tests such as used in if statements.

查看更多
何必那么认真
5楼-- · 2020-03-26 11:13

check bash command "expr"

For example, if you wnat to add two number: var=expr 2 + 6

Please refer to the man page for more expression.

查看更多
登录 后发表回答