Use shell for mathematical operations and shell mathematical operations

Source: Internet
Author: User

Use shell for mathematical operations and shell mathematical operations

Availablelet,(()),[]Basic arithmetic operations, advanced operationsbcMore efficient.

Let's look at the code and make the output clear.

Let
[Zhengweiwu ~] $ No1 = 4 [zhengweiwu ~] $ No2 = 5 [zhengweiwu ~] $ Let result = no1 + no2 [zhengweiwu ~] $ Echo $ result9 [zhengweiwu ~] $ Let result = result + 0.9 # There cannot be spaces on both sides of the operator-bash: let: +: syntax error: operand expected (error token is "+") [zhengweiwu ~] $ Let result = result + 0.9 # only integer operations can be performed-bash: let: result = result + 0.9: syntax error: invalid arithmetic operator (error token is ". 9 ") [zhengweiwu ~] $ Echo $ result9
[]
[zhengweiwu 22:26 ~]$ result=$[no1+no2][zhengweiwu 22:27 ~]$ echo $result9[zhengweiwu 22:27 ~]$ result=$[no1+no2+1][zhengweiwu 22:27 ~]$ echo $result10
(())
[zhengweiwu 22:28 ~]$ result=$((1+1))[zhengweiwu 22:28 ~]$ echo $result2[zhengweiwu 22:28 ~]$ result=$(( 10 +1))[zhengweiwu 22:29 ~]$ echo $result11
Bc
[Zhengweiwu ~] $ Echo "4*3.5" | bc14.0 [zhengweiwu ~] $ Echo "4*3.54" | bc14.16 [zhengweiwu ~] $ Result = $ (echo "$ no1 * 0.44" | bc) # used as the subshell computing result [zhengweiwu ~] $ Echo $ result1.76
[Zhengweiwu ~] $ Echo "scale = 2; 3/8" | bc # sets the decimal precision. 37 [zhengweiwu ~] $ Echo "scale = 4; 3/8" | bc.3750 [zhengweiwu ~] $ Echo "scale = 6; 3/8" | bc.375000
[Zhengweiwu ~] $ No = 100 [zhengweiwu ~] $ Echo "obase = 2; $ no" | bc # The output is in hexadecimal notation. The default input hexadecimal notation is in hexadecimal notation 1100100. [zhengweiwu ~] $ No = 1100100 [zhengweiwu ~] $ Echo "obase = 10; ibase = 2; $ no" | bc # specify the output hexadecimal and input hexadecimal 100
[zhengweiwu 22:34 ~]$ echo "sqrt(100)" | bc10[zhengweiwu 22:35 ~]$ echo "10^10" | bc10000000000

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.