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

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

小云云
Release: 2018-03-31 13:53:27
Original
2880 people have browsed it

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
Copy after login

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
Copy after login

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

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


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)
Copy after login

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
Copy after login



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
Copy after login



# 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
Copy after login


 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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template