Bash Digital Computing--Digital processing power is critical for any programming language, but for bash shell scripts, this ability is poor, even if the form of Expr command and $[] can only be used for integer operations, the operation of floating-point numbers cannot be completed. To break this limit, a bash floating point solution was introduced, which is built-in bash calculator (BC)
First, BC introduction
BC is actually a programming language that allows you to enter a floating-point expression at the command line and then execute it.
BC can recognize (floating-point) numbers, variables, expressions, programming statements, functions.
You can use the BC command to access bc,quit from the shell prompt to exit BC:
[Email protected] ~]# BCBC 1.06.95Copyright 1991-1994, 1997, 1998, $, 2004, 2006 free software Foundation, inc.this Free software with absolutely NO WARRANTY. For details type ' warranty '. 1.25*33.75quit
The scale variable sets the number of decimal digits , the default scale=0, and when scale=4, displays the answer to the 4-bit decimal digit:
[Email protected] ~]# Bc-q5/41scale=45/41.2500quit
Second, the use of BC in the script
Run the BC command using the inverse quotation marks
Var= ' echo ' options;expression ' |BC '
#!/bin/bashvar= ' echo ' Scale=4;5/4 "|BC ' echo $var
built-in input redirection run BC command
var= ' BC << EOF
Options
Expression
EOF '
#!/bin/bashvar= ' BC << eofscale=4a=5b=4b/aeof ' echo $var
Note: The variables in the BC are only valid in the Bash calculator and cannot be used outside of the BC
Floating-point calculation in shell scripts