Four of Shell Learning notes (integer operations)

Source: Internet
Author: User
Tags arithmetic

Tag:shell    integer arithmetic     operator    

The arithmetic operator shell only supports integer operations, and most common arithmetic operations are used in conjunction with the Shell's built-in command let. +-*/% * * (Power operation) + = = *=/=%= bit operations >> << & |    ^ ~ Self-increment minus + +--Example: let "b=3" let "a= (++b)" Other arithmetic operations simple arithmetic operations, if there is a value is a character, then resolve to 0                such as r+2=2 1, using $[] for the operation 1, to the present see the form of $ such as: ${} (Array) $[] (arithmetic operations) $ () (command substitution) 2, and $ (()) similar to the simple arithmetic example:                Echo $[1+1] echo $[2-1] echo $[2*2] echo $[5/2];//Discard decimals Echo $[5%2] Echo $[3**2] 2, use expr for Operation 1, integer operation 2, require a space between operands and operands, otherwise only the string will be printed, so special operators need to be escaped Character escapes (e.g. *) expr 1 + 1 expr 2-1 expr 2 \* 2 expr 2/1 If you get used to having spaces on both sides of the operator when programming, this is a good way to do it. , of course, note that "*" needs to be escaped 3, using the built-in command declare when declare is defined as shaping, the following string is parsed into an arithmetic example: Delcare-i num num    =1+3 echo $num Note: arithmetic operations in the shell require no spaces between operators and operands, but are tightly linked, where special symbols do not have to be escaped, and no $ references are required if the calculated wood expression contains other variables. 4, calculateThe number extension arithmetic extension is the operational mechanism of the integer variables provided by the shell and is one of the shell's built-in commands. Basic syntax: $ (arithmetic expression) where an arithmetic expression consists of variables and operators, the common use is to display output and variable assignments. If the variable in the expression is not defined, it is assumed to be 0 in the operation (the variable is not really assigned a value of 0) Example: i=2 echo $ ((i*3+3)) #i前没有 $ sign echo $ ((i+2        )) #用括号改变运算优先级 5, use BC to calculate the above operations are all based on integers.        BC: A high-precision computing language directly into the BC, enter the BC interface 1, the default BC does not display decimals, through the Scale=number set the number of decimal places displayed. 2, BC Support arithmetic operations, logical operations, Comparison operations 3, Support batch processing and processing expressions in the pipeline calculation example: 1, #cat CAL.BC a=2;                   B=5;a+b; #cat CAL.BC|BC 2, echo "2+3" |BC

Shell Learning Note Four (integer arithmetic)

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.