The assignment and operation in the shell are all string processing by default. Here are a few special methods for performing mathematical operations in the shell. You can see it later when you use it, huh, huh
1, the wrong method example a) var=1+1 echo $var output is 1+1, tragedy, huh, huh,
b) var=1 var=$var+1 echo $var output The result is 1+1, still tragedy, huh, huh
2, the correct method 1) use let var=1 let "var+=1" echo $var output is 2, this time no tragedy Note: a) After I tested let almost support all the operators, I saw an article on the Internet saying that "let does not support ++, --- and commas, (,) & rdquo;, but I tested self-add, decrement, and parentheses The priority is well supported. b) The power operation should use the "**” c) parameter to directly access the expression, without adding $ d) In general, the arithmetic expression can be without double quotes. However, if there are keywords in bash in the expression, you need to add e) the expression after the let can only perform integer operations
2) use (()) var=1 ((va r+=1)) echo $var The output is 2 Note: (()) is used exactly the same as let
3) Use $[] var=1 var=$[$var+1] echo $var output result bit 2 Note: a) $[] will use the expression in brackets as the mathematical operation to calculate the result and then output b) to access the variable in $[] before adding $ c)$[] support The operator is the same as let, but only supports integer operations
4) Use expr var=1 var=`expr $var + 1` echo $var The output is 2 Note: a) Expression after expr The symbols are separated by spaces. b) The operators supported by expr are: |
, &, <, <=, =, !=, >=, >, +, -, *, /, % c) The operators supported by expr need to be escaped using \\ Have:|
, &, <, <=, >=, >, * e) expr also supports only integer operations
5) use bc (can perform floating point calculation) var=1 var=` Echo "$var+1"|
Bc` echo $var output is 2 Introduction: bc is a simple calculator under linux, support floating point calculation, enter bc in the command line to enter the calculator program, and we want to directly calculate the floating point number in the program Use a simple pipeline to solve the problem. Note: 1) I tested bc to support all operators except the bitwise operator. 2) bc should use scale for precision setting 3) floating point number calculation example var=3.14 var=`echo "scale=2;$var*3"|
Bc` echo $var The output is 9.42
6) Use awk (which can be used for floating point calculation) var=1 var=`echo "$var 1"|
Awk '{printf("%g",$1*$2)}'` echo $var The output is 2 Introduction: awk is a text processing tool and a programming language, as a programming language. Awk supports a variety of operations, and we can use awk for floating point calculations. Just like bc above, we can call awk directly in the program for floating point calculation through a simple pipeline. Note: 1) awk supports all operators except micro-operators 2) awk has built-in functions such as log, sqr, cos, sin, etc. 3) floating point number calculation example var=3.14 var=`echo "$var 2" ;|
Awk '{printf("%g",sin($1/$2))}'` echo $var The output is 1 3. Summary Finally, all the materials that can be collected are read, and finally a set of their own Something, I will see similar problems later, haha~
When reading apue, the shared storage area is not used for instance. For this reason, I wrote a smal
I believe that many users who use Linux systems will feel that command line operations are faster th
What is the boot process for Linux? How to complete the system startup through grub boot? These pro
In the existing embedded LINUX system development process, all engineers are tired
Install and configure FTP service in CentOS
Linux Some concepts about dynamic link libraries and static link libraries
How to find recently modified files in Linux
The principle and function of Redo log
Linux system UMASK usage details
VPS installation openVPN basic tutorial
Linux server attack and defense technology detailed
What should I do if the WinXP system IE8 browser is automatically closed?
Developer Preview will add support for more than 20 devices
Win10 new photo application Learn to save the beautiful picture show
Baidu input method, traditional Chinese character fast switching
Windows 8 system ADSL idle line solution when idle
23 strokes to create speed Windows XP
Linux+Apache+Mysql+PHP build detailed instructions