Home  >  Article  >  Operation and Maintenance  >  Linux numerical operations: let, (()), [ ] detailed explanation

Linux numerical operations: let, (()), [ ] detailed explanation

小云云
小云云Original
2018-03-31 13:53:272836browse

In the Bash shell environment, you can use let, (( )) and [] to perform basic arithmetic operations. When performing advanced operations, the two tools expr and bc will also be very useful. You can use ordinary variable assignment methods to define a value, in which case it will be stored as a string. However, we can use some methods to make it operate like numbers


(1) The let command can directly perform basic arithmetic operations. When using let, there is no need to add $ before the variable name,

, for example,

[rhx@localhost Test]$ source 1.3.2.sh
[rhx@localhost Test]$ let result=no1+no2
[rhx@localhost Test]$ echo $result

self-increment, self-decrement, increment by step size

[rhx@localhost Test]$ let no1++
[rhx@localhost Test]$ let no1--
[rhx@localhost Test]$ let no1+=6
[rhx@localhost Test]$ let no1-=6

The usage of operator [] is similar to the let command:

[rhx@localhost Test]$ reslut=$[ no1+no2 ]


You can also use the $ prefix in [] , for example:

[rhx@localhost Test]$ reslut=$[ $no1+5

You can also use (()), but when using (()), the variable You need to add $ before the name:

result=$(( no1 + 50 ))
expr同样可以用于基本算术操作:
result=`expr 3 + 4`
result=$(expr $no1 + 5)

The above methods can only be used for integer operations and do not support floating point numbers.

(2) bc is an advanced tool for mathematical operations. This precision calculator contains a large number of options. You can use it to perform floating point operations and apply some advanced functions:

[rhx@localhost Test]$ echo "4*0.56" | bc



Other parameters can be placed before the specific operation to be performed, with semicolons as delimiters, Passed to bc via stdin.
Set decimal precision. In the example below, the parameter scale=2 sets the number of decimal places to 2. Therefore,

bc will output a value with two decimal places.

[rhx@localhost Test]$ echo "scale=2;3/8"|bc



# Base conversion. Use bc to convert one base system to another. Let's see how to convert decimal to binary, and then convert binary back to decimal:
#!/bin/bash

Purpose: Number conversion

[rhx@localhost Test]$ nu=100
[rhx@localhost Test]$ echo "obase=2;$nu"|bc


 Calculate squares and square roots.
echo "sqrt(100)" | bc #Square root
echo "10^10" | bc #Square

Related recommendations:

About Recommended articles related to numerical operators

The above is the detailed content of Linux numerical operations: let, (()), [ ] detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn