# 6. Numerical calculation of Shell variables

### 1. Data calculation commands for common variables

#### (1) Command

`(()),let,expr,bc,\$[],awk,declare`

#### (2) Description

bc: can calculate floating point numbers (decimals), others can only calculate integers. The most commonly used is (()), and the efficiency is also the highest.

### 2. Double brackets (()) command

 Operation Operators and Operation Commands significance ((i=i+1)) This method is a post-operation assignment method, you cannot use echo ((i=i+1)), you can use echo \$((i=i+1)) i=\$((i+1)) assign the value of the expression to i ((8>7&&5==5)) It can be compared, and logical and and logical or can also be added for conditional judgment echo \$((2+1)) When you need to directly output the operation result of the operation expression, you can add \$ before (())

#### (1) Example 1: Ordinary calculation

```[root@CentOS8 deploy]# ((a=1+2**3-4%3))
[root@CentOS8 deploy]# echo \$a
8
[root@CentOS8 deploy]# b=\$((1+2**3-4%3))
[root@CentOS8 deploy]# echo \$b
8
[root@CentOS8 deploy]# echo \$((1+2**3-4%3))
8                          ```

### 3. let command

#### (1) Format

let assignment expression, equivalent to ((assignment expression))

#### (2) Example 1: Basic usage of let

```[root@CentOS8 deploy]# i=2
[root@CentOS8 deploy]# i=i+8
[root@CentOS8 deploy]# echo \$i
i+8
[root@CentOS8 deploy]# i=2
[root@CentOS8 deploy]# let i=i+8
[root@CentOS8 deploy]# echo \$i
10
[root@CentOS8 deploy]# i=2
[root@CentOS8 deploy]# echo \$((i+8))
10```

### 4. expr command

#### (1) Description

The expr command is generally used with integer values, but can also be used with strings to evaluate the value of an expression variable. Meanwhile, expr is a manual command-line calculator. The format of the expr command is strict. There must be spaces before and after each parameter such as the operator of the expression and the calculated number (multiple spaces are fine), and the multiplication sign "*" needs to be escaped with "\".

#### (2) Format

`expr Expression`

#### (3) Example 1: Manual command line calculator

```[root@CentOS8 deploy]# expr 2 + 2
4
[root@CentOS8 deploy]# expr 2 + 1
3
[root@CentOS8 deploy]# expr 2-1 <--There are no spaces, it is treated as a string
2-1
[root@CentOS8 deploy]# expr 2 * 3 # <--need to be escaped
expr: syntax error
[root@CentOS8 deploy]# expr 2 \* 3 # <--need to be escaped
6```

#### (4) Example 2: Incremental counting

```[root@CentOS8 deploy]# i=0
[root@CentOS8 deploy]# i=`expr \$i + 1`
[root@CentOS8 deploy]# echo \$i
1```

#### (5) Example 3: Cooperate with []. It is actually the function of \$[], and the same is true with echo

```[root@CentOS8 deploy]# expr \$[2*3]
6
[root@CentOS8 deploy]# expr \$((2*3))
6
[root@CentOS8 deploy]# echo \$[2*3]
6```

#### (6) Example 6: Determine whether a variable is an integer (skill)

```[root@CentOS8 test]# cat expr.sh
#!/bin/bash

expr 1 + \$1 &> /dev/null

if [ \$? -eq 0 ]
then
echo "This is a zhengshu"
else
echo "This is not a zhengshu"
fi
[root@CentOS8 test]# sh expr.sh 1
This is a zhengshu
[root@CentOS8 test]# sh expr.sh 1.1
This is not a zhengshu```

### 5. bc command

#### (1) Description

bc is a calculator under UNIX that supports decimal calculations and can also be executed on the command line. Meanwhile, bc supports scientific computing.

#### (2) Example 1: Command line computing function (similar to python)

```[root@CentOS8 test]# bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
9-8
1
5/2
2```

#### (3) Example 2: Pass the pipeline command "|" to bc for calculation

```[root@CentOS8 test]# echo 1+1+1|bc
3
[root@CentOS8 test]# echo 1.2+2|bc  # < -- support decimal, normal calculation
3.2```

### 6. \$[] command

```[root@CentOS8 test]# echo \$[2+3]
5
[root@CentOS8 test]# echo \$[2 + 3]
5
[root@CentOS8 test]# echo \$[2    +    3]
5
[root@CentOS8 test]# echo \$[2    *   3]
6```

### 7, awk realizes the calculation

The effect of using awk for operations is also very good, suitable for decimals and integers, especially command-line calculations, especially decimals, the operation is very accurate.

```[root@CentOS8 test]# echo "7.7 3.8"|awk '{print (\$1-\$2)}'
3.9
[root@CentOS8 test]# echo "358 113"|awk '{print (\$1-3)/\$2}'
3.14159
[root@CentOS8 test]# echo "3 9"|awk '{print (\$1+3)*\$2}'
54```

Tags: shell

Posted by prouty on Tue, 02 Aug 2022 22:54:41 +0530