Numerical operations in the shell

Source: Internet
Author: User
Tags bitwise creative commons attribution

by Francis_hao Oct 2,2017

This article extracts the Man manual from bash.

The related forms of arithmetic operations

Form

Meaning

((expression))

expression is performed according to the arithmetic operation rules described below. If the final result is non-0, return 0. The other results return 1. This form and let "expression" equals

Let Arg [arg ...]

Each arg is an arithmetic expression, and if the last arg result is 0, 0 is returned, and the other result returns 1.

Arithmetic extension

The arithmetic extension allows arithmetic expressions to be computed and replaces the result. The form of the arithmetic extension is

$ ((expression))

Expression is considered to be in double quotation marks "", but the double quotation marks are not handled specially in parentheses. All items in expression are replaced by parameter extensions and commands. If expression is not valid, bash prints a message indicating that it failed. In addition, arithmetic expansions can be nested.

For example:

Where the arithmetic extension replaces the result directly, so $ (2) is the result.

Arithmetic operations

In certain environments, such as Let and arithmetic expansions, the shell allows arithmetic operations of numeric values, operations in fixed-length integers, and does not check for overflow. Divided by 0 is captured and marked as an error (printing an error message). Operators are consistent with their precedence, associativity, and C language, and parentheses can change precedence. The following table sorts by priority from highest to lowest.

Operator

Description

id++ d--

Self-increment, self-decrement (after the first value is evaluated)

++id--id

Self-increment, self-decrement (value after first operation)

  • +

unary minus and plus sign

! ~

Logical non-and bitwise non-

**

Powers

* / %

+ -

<< >>

<= >= < >

== !=

&

^

Bitwise XOR OR

|

&&

||

expr?expr:expr

= *=/=%= + = = <<= >>= &= ^= |=

EXPR1, EXPR2

Comma

In an expression, a shell variable can be referenced without a $, and a null or an undefined variable is treated as a%. A variable can set its integer property through the-I option of declare, but a variable in an arithmetic expression must not have an integer attribute because the default in an expression is treated as an integer.

Constants starting with 0 are interpreted as octal numbers, marked as 16 in the beginning of the 0x or 0X, and other systems can be represented by [Base#]n], where the optional base is a decimal number, between 0-64, which represents the binary. n is the specified number of digits, and if base# is omitted, decimal is used. Numbers greater than 9 are represented in order using lowercase letters, capitals, @, and _. The maximum is 64, that is, the maximum to be able to represent to 63, then 0-9 for 0-9,a-z means that 10-35,a-z means that 36-61,@ represents the 62,_ represents 63.


This article was authored by Liu Yinghao and licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 Chinese mainland license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: http://www.cnblogs.com/yinghao1991/p/7622777.html

Reference

"1" Scripting House http://www.jb51.net/article/57943.htm different binary data conversions in shell scripts

"2" stackoverflow.com https://stackoverflow.com/questions/27738943/what-is-unary-plus-minus-in-r

"3" Man sh

Numerical 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.