Using the shell for mathematical operations

Source: Internet
Author: User
Tags arithmetic

You can use, let (()) [] perform basic arithmetic operations, and use advanced operations bc more efficiently.

Look directly at the code and the output is clear.

Let
[Zhengweiwu A: -~]$ no1=4[Zhengweiwu A: -~]$ no2=5[Zhengweiwu A: -~]$ Letresult=no1+no2[zhengweiwu A: -~]$ echo $result9[Zhengweiwu A: -~]$ Letresult=result+0.9 # Operators cannot have spaces on either side-bash:let: +: SyntaxError: Operand expected (ErrorToken is "+") [Zhengweiwu A: -~]$ Letresult=result+0.9 # Only integer operations can be performed-bash:let:result=result+0.9: syntaxError: Invalid arithmetic operator (ErrorToken is ". 9") [Zhengweiwu A: -~]$ echo $result9
[]
[zhengweiwu 22  :  26  ~]$  Result=$[ no1+no2][zhengweiwu 22  :  27  ~]$  echo $ Result  9  [Zhengweiwu 22  :  27  ~]$  result=$[  no1+no2+1 ][zhengweiwu 22  :  27  ~]$  echo  $result  10   
(())
[Zhengweiwu A: -~]$ result=$((1+1)) [Zhengweiwu A: -~]$ Echo$result2[Zhengweiwu A: -~]$ result=$((Ten+1)) [Zhengweiwu A: in~]$ Echo$result One
Bc
[zhengweiwu 22 : 29  ~]$ echo  " 4*3.5 " | Bc14.0  [Zhengweiwu 22 : 29  ~]$ echo   "4*3.54 " | Bc14.16  [Zhengweiwu 22 : 30  ~]$ result=$ (echo   " $no 1  *0.44"  | BC) # as a child shell calculation result  [Zhengweiwu 22 : 30  ~]$ echo   $result  Span class= "Hljs-number" >1.76   
[zhengweiwu 22 : 30  ~]$ echo  " SCALE=2;3/8 " | BC # set decimal precision . 37  [Zhengweiwu 22 : 31  ~]$ echo   "SCALE=4;3/8"  | Bc. 3750  [Zhengweiwu 22 : 31  ~]$ echo   "SCALE=6;3/8"  | Bc. 375000   
[zhengweiwu 22 : 31  ~]$ no=100  [Zhengweiwu 22  : 33  ~]$ echo   "Obase = 2;  $no  " | BC # output is 2 binary, the default input is 10 binary  1100100  [Zhengweiwu Span class= "Hljs-number" >22 : 33  ~]$ no=1100100  [Zhengweiwu 22 : 33  ~]$ echo   $no   | BC # indicates output and input  100   
22:34echo"sqrt(100)" | bc1022:35echo"10^10" | bc10000000000

Using the shell for mathematical operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.