Strictly speaking, there is no concept of an expression in the shell. The shell itself is really just a collection of commands, and of course it is not a random heap, but a certain organization. But this organization is not so rigorous, so this article is not to really summarize the so-called expression, but the shell of some of the nooks and crannies together, I do not know that they belong to the classification.
- Command substitution
This is actually the effect of the anti-quote in this article. But there is another form of command substitution, as follows:
M@meng: ~/scripts$ m= ' Date ' m@meng: ~/scripts$ Echo$m -Years .Month -Day ThursdayTen: +: - CSTM@meng: ~/scripts$ n=$(Date) m@meng: ~/scripts$ Echo$n -Years .Month -Day ThursdayTen: +:Panax Notoginseng CST
That is, the ' command ' and $(command) effects are the same.
- Arithmetic substitution
As I said earlier, when a variable is declared as an integer type, it can do arithmetic operations, or the number assigned to the variable is no longer treated as a string, but as a real number. Without this integer declaration, the value of a numeric or arithmetic expression is still treated as a normal string when it is assigned to a variable.
But now, there is another way to achieve this effect, which is the symbol $ (expr). A variable in parentheses is treated directly as an integer without having to declare it as an integer type beforehand. As follows:
m @meng :~/scripts $ r=3 +4 m< Span class= "hljs-variable" > @meng :~/scripts $ echo $r 3 +4 m @meng :~/scripts $ echo $ ( ( $r ) 7 m @meng : ~/scripts $ echo $ ( ( $r +3 )) 10
However,$(()) can only be used with the +-*/and () operators, and only integer operations can be done.
- Quotes
The single and double quotation marks in a shell script are the same as the delimiter of the string, except that all the characters in the single quotation mark are treated as ordinary characters and discard their functional meanings. For example, ' $ ' is a variable value extract, but placed in single quotes is the literal value of ' \$ ', as follows:
m@meng:~/scripts$ q=5m@meng:~/scripts$ "$q"5m@meng:~/scripts$ ‘$q‘$q
Therefore, use variables in single quotes with caution.
The so-called qualifier, in fact, is to see the string inside the delimiter as a whole, without being interrupted by a delimiter such as a space.
In double quotes, all special characters will retain their escaped meanings, such as the '$' above. If you want a special character in a double quotation mark to revert to its true meaning, take its literal value, you can add the escape character ' \ ' in front of it, as follows:
m@meng:~/scripts$ "\$q"$q
- Exit status
In the shell, you can think of a command as an expression. Expressions are valued, and what is the value of a command? Is it the result of its execution? Like LS-listed files? In fact, the exit state of the command can be thought of as the value of the command expression: After the command is executed, there is an exit state to indicate whether the command is successful, and in general, the command execution in the shell has a successful exit status of 0, and a failure of 1 or another non-0 value. You can use the system variable "#?" To query the exit status of the previous command, such as:
M@meng: ~/scripts$ grep m New M@meng: ~/scripts$ Echo$?1M@meng: ~/scripts$ Cat New's M@meng: ~/scripts$ Echo$?0M@meng: ~/scripts$ m=4M@meng: ~/scripts$ Echo$?0
In the above example, the grep command does not find the character "M" in the new file, so its exit status is 1, and the other is 0, including the assignment statement.
The exit state is often used as the judgment condition for the IF statement.
- The
-
Test expression
can use the exit state of the command directly as a condition for a statement such as if, while, but it is more commonly used as test commands such as tests. The syntax for the
Test command is:
The test expression
test is primarily used to perform three types of tests: file properties, numeric comparisons, string comparisons. If the test result is true, the exit status of the command is 0, and if the test result is false, the exit status of the command is 1. The
-
File Test
Format is: Test options file. The main options include the existence of
- -E test file, which is 0 if present, otherwise 1. This result is consistent with the exit status of the normal command.
- -D tests whether the file is a directory, is 0, or no is 1.
- -F If file is a normal document, the result is true (that is, the value is 0).
- -S tests whether the file is non-empty, 0 if file exists and is not NULL, otherwise, it does not exist or is an empty file, which is 1.
- -R file exists and is read-only, true.
- -W file exists and is writable, true.
- -x file exists and is executable, true.
-
Numeric test
The comparison operator for numeric tests uses text, which is compared to the following (the traditional operator used by the C language on the left side is the shell used by the right):
= =:-eq
! =:-ne
>:-GT
\<:-lt
>=:-ge
\<=:-le
-
String test
-Z str: If the length of STR is 0, the result is true (remember: true <== > $? = = 0)
-n str: If the length of STR is not 0, the result is true
S1 = S2: Two strings are equal, the result is true
S1! = S2: Two strings, the result is true
Str: no option to test str, effect with-n
/li>
[] Expression
In fact, only [is the command, and] is only the delimiter. [The command is basically the same as the test command, the only difference is that the format is different, [the command needs to put the expression to be tested in parentheses, # # #且表达式左右两边分别要和 [] Leave a Space # # #, such as [-Z ' empty]. It is said that [the command is a little more efficient, I have not verified it.]
Another point: The ###[command does not support the operator ">" and "<" # # #, so support for string comparison operations may be nearly, which is the only two difference ....
[[]] expressions
expanded the [] command, the first is the support of the operator ">" and "<", in addition to support the logical operation &&,| |,!, as follows:
m@meng:~/scripts$ [ er > et ]m@meng:~/scripts$ $?0m@meng:~/scripts$ [[ er > et ]]m@meng:~/scripts$ $?1
Obviously, the ER > et results should be false, so the exit state of the test expression is 1. But [] there is no effect, and [[]] the test is correct.
m @meng :~/scripts Span class= "hljs-variable" >$ [er > et | | er < et]bash: [ Missing '] er: command not found M @meng :~/scripts $ [[er > et | | er < et]]m @meng Span class= "Hljs-symbol" >:~/scripts $ echo $ ? 0 m @meng :~/ Scripts $ [[er > et && er < et]]m@ Meng :~/scripts $ echo $? 1
- arithmetic expression
- (()) expression
seems a little messy, the difference between (()) and $ (()) is that the latter can extract the result of the calculation as a value, and I personally feel, ' $ The ' symbol is used to extract the value, such as when referencing a variable. and the symbol (()) is just the completion of the calculation itself, as to save the results, although it can, but is not the main business. Or just look at the command:
m@meng:~/scripts$ ((m=4-2))m@meng:~/scripts$ $m2m@meng:~/scripts$ m=$((4-2))m@meng:~/scripts$ $m2
This means that ((m=4-2)) and m=$ ((4-2)) The effect is the same, meaning that the assignment can be done directly inside (()).
~~~~
m@meng:~/scripts$ $((4-2))2m@meng:~/scripts$ echo ((4-2))bash:‘(‘ 附近有语法错误
This indicates that (()) is not responsible for saving the results, if desired results, with ' $ ' to extract.
~~~~
m@meng:~/scripts$ m=3;n=2m@meng:~/scripts$ $((m-n))1
This means that you do not need to add a ' $ ' symbol when using variables inside (()).
~~~~
m@meng:~/scripts$ m=3;n=2m@meng:~/scripts$ $((m<n))0m@meng:~/scripts$ $((m>n))1
Obviously, (()) also supports the comparison operation, the only problem is that its exit status is exactly the opposite of other commands like Test !
~~~~
Summarize this expression:
1, only support integer arithmetic;
2, supported operations include: = (Assignment), = = (equal), >= <= < >% +-*/= + =/= *=%=, very rich.
- Let expression
Just like the one above, the format is different, and the Let expression is written behind let, not in parentheses. In addition, since there are no natural qualifiers like (()), you need to enclose them in quotation marks when there are spaces in the calculation.
- Expr expression
Write again later, so tired ... The above is enough.
Logical operations
There are &&, | |,-A,-O,!, and (), which are used to calculate the logical operation between expressions, supported by parentheses (), with spaces between the logical operators and expressions .
The [[]] command supports only symbols and does not support the-A and-O options.
[[ er > et -o er < et ]]‘-o‘ 附近有语法错误
Finish.
The expression of the Linux shell