Linux shell script uses bc command to implement decimal numerical calculation

Table of contents

Problem background

Idea analysis

Solution


Problem background

Usually when using shell scripts to calculate decimal addition, subtraction, multiplication and division in actual work, the calculation results need to be saved in variables.

Idea analysis

Use the bc method, and use backticks to create new variables to execute subsequent calculation commands (the key below esc in the upper left corner of the keyboard)

Solution

a=100
var=`echo $a+0.1 | bc`
var=`echo $a-0.1 | bc`
var=`echo $a*0.1 | bc`
var=`echo $a/0.1 | bc`

Guess you like

Origin blog.csdn.net/qq_35902025/article/details/129946352