Double brackets (( )) are commands specially used in Bash Shell for integer operations. They are very efficient and flexible in writing. They are commonly used in enterprise operations and maintenance.
Note: (( )) Only integer operations can be performed, and decimals (floating points) or strings cannot be performed. The bc command mentioned later can be used for decimal operations.
How to use Shell (( ))
The syntax format of double brackets (( )) is:((expression))
In layman's terms, it means putting mathematical operation expressions between ((and)).
There can be only one expression or multiple expressions, separated by commas. For multiple expressions, the value of the last expression is used as the execution result of the entire (( )) command.
You can use $ to get the result of the (( )) command, which is similar to using $ to get the variable value.
Table 1: Usage of (( ))
Operation operator/operation command | illustrate |
---|---|
((a=10+66) ((b=a-15)) ((c=a+b)) |
This writing method can assign values to variables after calculation is completed. Taking ((b=a-15)) as an example, the calculation result of a-15 is assigned to the variable c. Note that you do not need to prefix $ when using variables, (( )) will automatically resolve the variable name. |
a=$((10+66) b=$((a-15)) c=$((a+b)) |
You can add the $ symbol before (( )) to get the execution result of the (( )) command, that is, get the value of the entire expression. Taking c=$((a+b)) as an example, assign the calculation result of the expression a+b to the variable c. Note that writing methods like c=((a+b)) are wrong, and the result of the expression cannot be obtained without adding $. |
((a>7 && b==c)) | (( )) You can also perform logical operations, and logical operations are often used in if statements. |
echo $((a+10)) | When you need to output the calculation result of the expression immediately, you can add the $ sign before (( )). |
((a=3+5, b=a+10)) | Compute multiple expressions simultaneously. |
When using variables in (( )) without prefixing $, (( )) will automatically parse the variable name, which makes the code more concise and conforms to the writing habits of programmers.
Shell (( )) example demonstration
[Example 1] Use (( )) to perform simple numerical calculations.
[]$ echo $((1+1)) 2 []$ echo $((6-3)) 3 []$ i=5 []$ ((i=i*2)) # can be abbreviated as ((i*=2)).[]$ echo $i # When using echo to output variable results, add $.10
[Example 2] Use (( )) to perform slightly more complex comprehensive arithmetic operations.
[]$ ((a=1+2**3-4%3)) []$ echo $a 8 []$ b=$((1+2**3-4%3)) #After the operation, assign the result to the variable, and the variable is placed outside the brackets.[]$ echo $b 8 []$ echo $((1+2**3-4%3)) # You can also directly output the result of the expression, be careful not to throw away the $ symbol.8 []$ a=$((100*(100+1)/2)) #Use the formula to calculate the sum of 1+2+3+...+100.[]$ echo $a 5050 []$ echo $((100*(100+1)/2)) # can also directly output the result of the expression.5050
[Example 3] Use (( )) to perform logical operations.
[]$ echo $((3<8)) The result of #3<8 is true, so 1 is output, 1 means true1 []$ echo $((8<3)) The result of #8<3 is invalid, so 0 is output, and 0 means false.0 []$ echo $((8==8)) #Judge whether it is equal.1 []$ if ((8>7&&5==5)) > then > echo yes > fi yes
Finally, there is a simple if statement format, which means that if 8>7 holds, and 5==5 holds, then output yes. Obviously, both conditions are true, so yes are output.
[Example 4] Use (( )) to perform self-increment (++) and self-decrease (--) operations.
[]$ a=10 []$ echo $((a++)) #If ++ is behind a, then when outputting the entire expression, the value of a will be output. Since a is 10, the value of the expression is 10.10 []$ echo $a #After executing the above expression, because there is a++, a will increase by 1, so the value of output a is 11.11 []$ a=11 []$ echo $((a--)) #If -- after a, then when outputting the entire expression, the value of a will be output. Since a is 11, the value of the expression is 11.11 []$ echo $a #After executing the above expression, because there is a--, a will automatically subtract 1, so a is 10.10 []$ a=10 []$ echo $((--a)) #If--before a, then when outputting the entire expression, the self-increment or self-decrease calculation is performed first. Because a is 10 and needs to be self-decreased, the value of the expression is 9.9 []$ echo $a #After executing the above expression, a is subtracted by 1, so a is 9.9 []$ echo $((++a)) #If ++ is in front of a and output the entire expression, first perform self-increment or self-decrease calculations. Because a is 9 and needs to be self-incremented, 10 is output.10 []$ echo $a #After executing the above expression, a is incremented by 1, so a is 10.10
This tutorial assumes that readers have basic programming skills. I believe that readers are very clear about the difference between the first self-increase (previous self-decrease) and the second self-increase (after self-decrease). I will not repeat it here, but only briefly explain it:
- When the echo $((a++)) and echo $((a--)) commands are executed, the output value is the value of a. After the expression is executed, a will be performed in ++ and -- operations on a;
- When executing the echo $((++a)) and echo $((--a)) commands to output the entire expression, a will first perform ++ and -- operations on a, and then output the value of the expression, which is the value after a operation.
[Example 5] Use (( )) to calculate multiple expressions simultaneously.
[]$ ((a=3+5, b=a+10)) #First calculate the first expression, then calculate the second expression[]$ echo $a $b 8 18 []$ c=$((4+8, a+b)) #The result of the last expression is used as the execution result of the entire (()) command[]$ echo $c 26
This is the end of this article about Shell(()) implementing mathematical operations on integers. For more related Shell(()) Integer operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!