Mathematical operations in the shell

Source: Internet
Author: User
Tags arithmetic

There are usually 3 ways to perform mathematical operations in the shell:

Expr command

For example, expr 1 + 6 will return 7, the disadvantage of using expr is to encounter multiplication, or parentheses (because they have other meanings in the shell), need to use escape, such as: Expr \ (2 + 3\) \* 5, which makes the expression difficult to see

$[expression] Command

Using the $[expression] command can also be mathematically calculated, and it is easier than expr that the * and parentheses can be escaped without escaping, such as still calculating (2 + 3) * 5, which can be directly written as:

result=$[(2 + 3) * 5]

both of the above operator expr commands and $[expression] can handle only integers, and cannot handle floating-point numbers, and you must use the BC command in the shell to perform floating-point arithmetic.

BC Command

For simple operations, you can use the following form:

Result= ' echo ' options;expression ' | BC '

The options usually use the variable scale inside the BC to set the floating-point precision (the scale default is 0), expression is the arithmetic expressions, such as the calculation of 3.6 * 7.2, can be written as:

Result= ' echo ' scale=3;3.6 * 7.2 | BC '

This form is convenient for simple calculations, but if it comes to complexity, you can use the following form:

result= ' BC << EOF

Options

Expression

EOF '

Where << is an inline input redirection operator, followed by a closing flag (which can be any character or string), the expression above indicates the input to the BC command until EOF is encountered; options and expression ibid.

For example, the calculation is still 3.6 * 7.2, which can be written as:

result= ' BC << eofscale = 3var1 = 3.6VAR2 = 7.2var1 * var2eof '

There are two points to note when using the BC command:

1) The BC command can define its own variables, such as the above var1 and var2, but can only be used in the BC Command, when the BC command ends, these variables are inaccessible, and in the BC access to these variables without the $ symbol

2) The BC command can access variables in the shell

Mathematical operations in the shell

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.