![bef1493ed52301cabf98cc33b5791d6c.png](https://img-blog.csdnimg.cn/img_convert/bef1493ed52301cabf98cc33b5791d6c.png)
【135】
一、变量
二、引用&命令替换
三、退出状态&逻辑判断
四、表达式
一、变量
- 本地变量
- 当前shell拥有 举个例子 $LANG = xxx 再次登录就没了,其他ssh连接也不能共享
- 生命周期随shell
- 局部变量
- 只能local用于函数
本地局部变量演示:
![cd4d881f1b8c310066a1026035e1cb1a.png](https://img-blog.csdnimg.cn/img_convert/cd4d881f1b8c310066a1026035e1cb1a.png)
- 位置
- $1,$2,${11}
- 脚本中使用
- 函数中使用
- 传给前面的命令加‘ ’ 如 awk命令。不加单引号传给bash
位置变量演示:位置变量示例1:(脚本中用两个位置 1、2来接受外部的两个参数)
![bd8d295f9099a10729f6d7759e453676.png](https://img-blog.csdnimg.cn/img_convert/bd8d295f9099a10729f6d7759e453676.png)
位置变量示例2:(大括号“{}”括起来)
![6bbfa5748c0fc1e3cd23876395cbf80e.png](https://img-blog.csdnimg.cn/img_convert/6bbfa5748c0fc1e3cd23876395cbf80e.png)
位置变量示例3:(函数中使用)
![41dbb0919dc5713e157d1f38a068d4b6.png](https://img-blog.csdnimg.cn/img_convert/41dbb0919dc5713e157d1f38a068d4b6.png)
位置遍历示例4:(传给bash不加 ‘ ’ ,传给前面的命令加‘ ’)
![b75973905d1ef608099ae0068422c778.png](https://img-blog.csdnimg.cn/img_convert/b75973905d1ef608099ae0068422c778.png)
- 特殊
- $#:位置参数个数
- $*:参数列表,双引号引用为一个字符串
- $@:参数列表,双引号引用为单独的字符串
![c1dce62b4d8bd47f65f49e22d8da0818.png](https://img-blog.csdnimg.cn/img_convert/c1dce62b4d8bd47f65f49e22d8da0818.png)
-
- $$:当前shell的PID:接收者
- $BASHPID:真实
- 管道 管道也是一个比较特殊的东西。使用管道左右两边会分别开启一个子bash
- $$:当前shell的PID:接收者
![8f2fcc29b2606544b364313934dae2f3.png](https://img-blog.csdnimg.cn/img_convert/8f2fcc29b2606544b364313934dae2f3.png)
![ce829a1e66fc54e4bd4f72d019d9579d.png](https://img-blog.csdnimg.cn/img_convert/ce829a1e66fc54e4bd4f72d019d9579d.png)
-
- $?:上一个命令退出状态
- 0:成功
- other 非0:失败
- $?:上一个命令退出状态
![63c918823ea5a228b919c5b6d142614d.png](https://img-blog.csdnimg.cn/img_convert/63c918823ea5a228b919c5b6d142614d.png)
- 环境
- export 环境变量中使用,原理就是将变量导出共享到任何子bash中执行。
![53d90b76b4794fe0c2c1056e583198f3.png](https://img-blog.csdnimg.cn/img_convert/53d90b76b4794fe0c2c1056e583198f3.png)
![28358732416cfef3ef349b484a95ff3c.png](https://img-blog.csdnimg.cn/img_convert/28358732416cfef3ef349b484a95ff3c.png)
-
- 导入的父bash变量并非实时共享的
![722326136710d59414ac1e0e6f9be899.png](https://img-blog.csdnimg.cn/img_convert/722326136710d59414ac1e0e6f9be899.png)
二、引用&命令替换
- 引用:
- 双引号:弱引用,参数扩展
- 单引号:强引用,不可嵌套
- 单双引号的区别:
![96b021dcbb6849c78b17f0bff09ec569.png](https://img-blog.csdnimg.cn/img_convert/96b021dcbb6849c78b17f0bff09ec569.png)
-
- 花括号扩展不能被引用
![8730b483c533a8ddb5744e1c648fc0cb.png](https://img-blog.csdnimg.cn/img_convert/8730b483c533a8ddb5744e1c648fc0cb.png)
-
- 命令执行前删除引用
![456b5ab8ec2638f687fbc4a1cf8400c4.png](https://img-blog.csdnimg.cn/img_convert/456b5ab8ec2638f687fbc4a1cf8400c4.png)
- 命令替换:
- 反引号:`
![f9144e2f5fd9f52259596bc62dc8c8ce.png](https://img-blog.csdnimg.cn/img_convert/f9144e2f5fd9f52259596bc62dc8c8ce.png)
-
- $(ls -l /)
![d64d57fbf891af16709dabed131b562a.png](https://img-blog.csdnimg.cn/img_convert/d64d57fbf891af16709dabed131b562a.png)
-
- 可以嵌套
三、退出状态&逻辑判断
- 退出状态
- echo $? 0表示成功,非0表示失败。可以用在脚本里面进行逻辑判断。
- 逻辑判断
- command1 && command2 短路与
- command1 || command2 短路或
![de8ece13f7876acf6bf2035c259d62f2.png](https://img-blog.csdnimg.cn/img_convert/de8ece13f7876acf6bf2035c259d62f2.png)
四、表达式
- 算术表达式
- let 算术运算表达式
- let C=$A+$B
- let 算术运算表达式
-
- $[算术表达式]
- C =$[$A+$B]
- $[算术表达式]
-
- $((算术表达式))
- C=$(($A+$B))
- C=$((A+B)) 内层$可以省略
- ((A++)) 当某个值只改变不复制给其他变量,格式为去掉$
- expr 算术表达式
- 注意:表达式中各操作数及运算符之间要有空格。而且要使用命令引用
- C=
expr $A + $B
- $((算术表达式))
- help let一定要打开看看里面的各种表达式表示
- 演示:
![d614fc1993e36073f60701c8ca5f8878.png](https://img-blog.csdnimg.cn/img_convert/d614fc1993e36073f60701c8ca5f8878.png)
![69154a38c57494b17152a9bca481b322.png](https://img-blog.csdnimg.cn/img_convert/69154a38c57494b17152a9bca481b322.png)
- 条件表达式
- [ expression ]
- test expression
- [[ expression ]]
- help test 可以判断文件/字符串 >、 < 号的问题,一定要打开查看下
- 演示:
![366fbb8eba79cd37edc68dce86df00c4.png](https://img-blog.csdnimg.cn/img_convert/366fbb8eba79cd37edc68dce86df00c4.png)