Shell arithmetic operations

Source: Internet
Author: User
Tags arithmetic

The coffee is too much to drink. I guess I'll have no sleep tonight. Mom, Egg.

The arithmetic operation of the shell. There are too many. There are many tools available to download on the Web. This article only selects the commonly used to carry on the introduction.

1. $[] This may be less useful. I was also curious when I first saw this one thing. But it really works. It is limited to shaping. Cannot be used to calculate floating-point numbers.

The disadvantage is that the calculation has limitations. Seemingly functional computational subtraction and modulo operations.

Note: * * represents a power operation.

2. $ (()) I like to call this C-language mode. Either in the conditional judgment or in the loop, or here. There is no limit to the variables. This expression is not available for floating-point numbers.

3. Let command. As the name implies is to make var1=[...]. This is understood because let's left and right values are on the right side of let. Only shaping. Floating-point numbers are not available.

Note that the expression following the let cannot have spaces.

4. The expr command. And let are different. The left value is on the left of expr and the right is the expression. Only for shaping, floating-point numbers are not available.

Note that spaces are required between the arguments after expr.

I hope the above example will not be misleading. Those operations are just what I know. There can be other operations, such as bitwise operations.

The above command we can see one thing in common, that is, the floating-point number cannot be calculated, and the computational power is limited. But in reality these simple functions are far from satisfying our needs. What to do.

Of course, students can go online to find a calculator, even with UI interface can be. Here's a super powerful computer: BC

It is said that there is no operation it can not complete. I don't know if it's true.

5. BC differs from the above command, which receives an expression from a standard input and then outputs the result to standard output.

In fact, I know a lot about BC. Many have to rely on man to find. and take the system of this function, it seems that man can not find. I searched the internet for a long time before I found it. That is, obase and IBase, respectively, representing the input and output inputs.

BC supports conditional judgment, shift operation. Some functions are implemented. Settings can use the for and other process control statements. It is very profound. But I have mastered the fur.

Of course, Linux commands a lot more than that, for example, you can enter Calc. The character interface will pop up to perform the operation. But I just accumulate these kinds of ways.

Shell arithmetic 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.