Shell Basics 03 Mathematical operations

Source: Internet
Author: User

There are some limitations to mathematical operations in the shell. It is mainly divided into the following types:

1. square brackets-------support only integer arithmetic

[[email protected] documents]$ var1=$[1+5]

[email protected] documents]$ echo $var 1

6
[Email protected] documents]$ var2=$[$var 1*2]
[Email protected] documents]$ echo $var 2
12
[Email protected] documents]$ var1=100
[Email protected] documents]$ var2=45
[[email protected] documents]$ var3=$[$var 1/$var 2]
[Email protected] documents]$ echo $var 3
2

2. Floating point Solution

Use the built-in bash calculator directly, called BC. It supports multiple types of calculations

Floating-point computing is designed to be a problem of precision, and you can set the value of the variable scale to fit the precision we want. For example, scale=4, meaning that 4-bit output is retained after the decimal point

BC can handle other data types, time reference.

Reference Documents :

Linux command line and Shell Scripting Encyclopedia (3rd edition) [Mei] Bloom (Richard Blum), Bresnahan (Christine Bresnahan), Jia, Wuhai

Shell Basics 03 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.