1. The prototype of variables in Shell: ${var} is the execution of a string of commands ( instead of variables )
#Equivalent to $ var = test $ echo $ var test #For example, use in this position $ echo $ {var} AA testAA
2. Command substitution $(cmd) Execution of a string of commands ( replacement commands )
The command substitution $(cmd) is the same as the symbol `cmd` (note that this is not a single quote, on a US keyboard, ` is the key below ESC)
$ ls a b c $ echo $ ( ls ) a b c $ echo ` ls` _ a b c
Let's analyze the command echo $(ls) in order to understand what the so-called command substitution means:
The shell scans the command line once, finds the $(cmd) structure, executes the cmd in $(cmd) once, and gets its standard output , and then put this output into the $(ls) position in the original command echo $(ls), that is, replace $(ls), and then execute the echo command.
() and {} both execute a series of commands, but there are differences:
A,() just re-open a subshell to execute
a series of commands B, {} execute a series of commands in the current shell
3. Extended calculation of POSIX standard: $((exp)) is used for mathematical calculation ( comparison and operation of integers )
This calculation is a C-compliant operator, that is, as long as the C-compliant operator can be used in $((exp)), even the ternary operator.
Note: This extended calculation is an integer calculation and does not support floating-point type. If it is a logical judgment, the expression exp is true, it is 1, and it is 0 if it is false.
$ echo $(( 3 + 2 )) 5 $ echo $(( 3 > 2 )) 1 $ echo $(( 25 < 3 ? 2 : 3 )) 3 $ echo $var $ echo $((var=2+3)) 5 $ echo $var 5 $ echo $((var++)) 5 $ echo $var 6
4、单中括号 [] 对于转义字符要加\ eg:[ \($INT\)]
bash 的内部命令, [ 和 test 是等同的。
Test和[]中可用的比较运算符只有==和!=,两者都是用于字符串比较的,不可用于整数比较,整数比较只能使用-eq,-gt这种形式。无论是字符串比较还是整数比较都不支持大于号小于号。
因为[ 是Linux内部命令,所以 [ a+b ]之间要有空格。
5、双中括号[[ ]] 不用加斜杠转义eg: [[ ($INT) ]] 较常用
[[是 bash 程序语言的关键字。使用[[ ... ]]条件判断结构。比如,&&、||、<和> 操作符能够正常存在于[[ ]]条件判断结构中,但是如果出现在[ ]结构中的话,会报错。
4、5例如:
if ($i<5) if [ $i -lt 5 ] if [ $a -ne 1 -a $a != 2 ] if [ $a -ne 1] && [ $a != 2 ] if [[ $a != 1 && $a != 2 ]] for i in $(seq 0 4);do echo $i;done for i in `seq 0 4`;do echo $i;done for ((i=0;i<5;i++));do echo $i;done for i in {0..4};do echo $i;done
https://my.oschina.net/tridays/blog/807162
最后,推荐使用 [[
来进行各种判断,这能避免很多错误,比如 $a
为空的情况下,[ $a == 1 ]
就是语法错误,因为 [
命令拿到的实际上只有 ==
、1
、]
三个参数。即:[[]]常用些。
$ [ 1 < 2 ] zsh: no such file or directory: 2 #------------------------------------- $ [[ 1 < 2 ]] && echo True || echo False True
首先,来看下shell中$与各种括号的结合:
$( )运行shell命令返回输出
如$(ls -a)
$(( )) 算术运算
如$(($a+$b))
$[] 算术运算
如$[$a+$b]
${ } 变量调用
如${$var}
再来看看单独使用的情况:
( ) 正则表达式中可以表示分组,并在后面通过\1,\2等调用
[ ] 测试表达式
[[ ]] 测试表达式
{ }可用于括起整个命令块
实在是不好记,于是,小林君又反过来总结变量调用、命令调用、测试表达式和算术运算的方法:
变量调用:
方法一:${var}
方法二:$var
命令调用:
方法一:`COMMAND`
方法二:$(COMMAND)
测试表达式:
方法一:[ expression ]
方法二:[[ expression ]]
方法三:test expression
算术运算
方法一:let 算术运算表达式
let C=$A+$B 这里变量A和B前面的$可以省略
方法二:$[算术运算表达式]
C=$[$A+$B]
方法三:$((算术运算表达式))
C=$(($A+$B))
Method 4: expr arithmetic operation expression, there should be spaces between the operands and operators in the expression, and command references should be used, and the * sign in multiplication should be escaped
C=`expr $A + $B`
Method 5: expr $[arithmetic operation expression], no need to escape the * sign when encountering multiplication.