Bash supports many operators, including arithmetic operators, relational operators, Boolean operators, string operators, and file test operators.
Native Bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.
expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.
For example, two numbers are added:
#!/bin/bash
caoliu= ' Expr 2 + 2 '
Echo $caoliu
Two points Note:
There are spaces between the expression and the operator, such as the 2 + 2, which is different from most of the programming languages we are familiar with.
The complete expression is to be contained, note that this character is not a common single quote, below the ESC key.
Arithmetic operators
Let's take a look at an example that uses arithmetic operators:
#!/bin/sha=10b=20val= ' expr $a + $b ' echo ' A + B: $val ' val= ' expr $a-$b ' echo ' A-B: $val ' val= ' expr $a \* $b ' echo ' A * : $val "val=" expr $b/$a ' echo ' b/a: $val "val=" expr $b% $a ' echo ' B% A: $val "if [$a = = $b]then echo" a equals B "Fiif [$ A! = $b]then echo "A not equal to B" fi
Results:
A + b:30
A-B: 10
A * b:200
B/A: 2
B% a:0
A is not equal to B
Attention:
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1793549
10.Shell operator