Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
When you use Linux, you’ll most likely need to do a simple calculation of a few pieces of data.
It could be a small temporary calculation, or it could be a simple execution from somewhere else, and if it can be done on the shell, look at the results. Will be very convenient (no need to go to the pen calculation, calculator software…) .
Here are some basic operations in the shell!
introduce
Operators for computation are provided under the terminal shell, which is the domain of shell programming.
In addition, there are direct commands for computation, such as let, expr, and BC commands that support floating-point arithmetic.
The let command
The let command enables simple arithmetic and logical operations. You can use help let to see how to use it.
The let command assigns the result of an arithmetic operation to a variable.
Operators in the let command must not be preceded by Spaces.
Execute directly from shell terminal as follows:
$ i=1
$ echo $i
1
$ let i=i+10
$ echo $i
11
$ let "i=i+100"
$ echo $i
111
Copy the code
You can also put it in a shell script file as follows:
#! /bin/sh
i=1
echo $i
let i=i+10 # 11
echo $i
let "i=i+100" # 111
echo $i
Copy the code
The script name is calc.sh:
$ bash calc.sh
1
11
111
Copy the code
[]
[] equivalent to the let command. Specific examples are as follows:
$ i=1
$ i=$[i+10]
$ echo $i
11
$ i=$[i+100]
$ echo $i
111
Copy the code
(())
(()) is equivalent to []. Usage:
$ i=1
$ i=$((i+10))
$ echo $i
11
$ i=$((i+100))
$ echo $i
111
Copy the code
Expression expr
Expr is an expression evaluation tool, used to complete expression evaluation operations.
Note: Expr expressions need to be placed in backquotes, not single quotes, and operators must be preceded by Spaces, otherwise the operation cannot be performed.
$ i=`expr 1 + 10`
$ echo $i
11
$ i=`expr $i+ ` $100echo $i
111
$ i=`expr 1 / 10`
$ echo $i
0
Copy the code
If there is no space before or after the operator, it will print as is:
$ i=`expr 1+10`
$ echo $i1 + 10Copy the code
BC command (supports interactive execution of computations with arbitrary precision)
All of the above calculations apply only to integer calculations. If you want to do decimals, floating point operations, you can use the BC command.
The BC command is a calculator language that supports interactive execution with arbitrary precision.
Bash has built-in support for only four operations on integers. It does not support floating-point arithmetic, and BC can easily implement floating-point arithmetic. Some functions are also supported.
BC command parameters:
-i: forcibly enters the interactive mode.
-l: defines the standard math library to use.
When BC is executed, specify scale= INTEGER to set the decimal number, or precision, of the output.
BC is called Binary Calculator.
The default operation
When the default operation is performed, the result is as accurate as the operand:
$ echo "1.212960 * 3" | bc
3.638880
$ echo "1.21296 * 3" | bc
3.63888
$ echo "1.21296 * 3 + 2" | bc
5.63888
$ echo "1.21295 * 2" | bc
2.42590
Copy the code
Specify scale precision
Specify scale precision:
$ echo "scale=7; 3/8" | bc
.3750000
$ echo "scale=7; 10/3" | bc
3.3333333
Copy the code
(using backquotes) gets the result of BC
Get the result of the BC execution using backquotes:
$ i=10
$ j=3
$ n=`echo "scale=7; $i/$j" | bc`
$ echo $n
3.3333333
Copy the code
The BC command does not require Spaces before or after the operator.
Obase base conversion
In BC, conversion to base can be implemented by specifying obase
Convert the default decimal 192 to a binary value as follows:
$ abc=192
$ echo "obase=2;$abc" | bc
11000000
Copy the code
Binary to decimal:
$ let abc=2# 11000000
$ echo "obase=10;$abc" | bc
192
Copy the code
Here, you need to convert the following binary representation to a variable using the let command.
Calculate the square and square roots
$ echo "10 ^" | bc
10000000000
$ echo "sqrt(100)" | bc
10
Copy the code
reference
Main references: Shell programming — math in the Shell, BC commands, and other resources.