shell變量java
1、變量分類shell
系統變量: $HOME、$PATH、$PWD等。數組
自定義變量:用戶定義的變量ide
2、變量定義的語法spa
定義:變量名=值(不能有空格)命令行
撤銷:unset 變量名3d
靜態變量:readonly 變量名=值(不能unset)blog
![1563512914196277.png image.png](http://static.javashuo.com/static/loading.gif)
![1563512921493600.png image.png](http://static.javashuo.com/static/loading.gif)
3、命名規則進程
1)字母、數字、下劃線,不以數組開頭it
2)等號兩邊不能有空格
3)變量通常大寫
4、變量賦值Linux指令
1)變量名=`指令`(` `爲反引號)
2)變量名=$(指令)
![1563512938736957.png image.png](http://static.javashuo.com/static/loading.gif)
![1563512944612467.png image.png](http://static.javashuo.com/static/loading.gif)
5、環境變量設置
1)export 變量名=值(設置環境變量)
2)source 配置文件 (配置文件生效)
3)echo $變量名 (打印配置變量)
![1563512971811136.png image.png](http://static.javashuo.com/static/loading.gif)
![1563512979609606.png image.png](http://static.javashuo.com/static/loading.gif)
6、位置參數變量
對終端命令行中的參數處理,相似java中的方法調用。
$n: n爲數字,0指命令自身,1~n爲參數位置,10以上${n}
$*: 全部的參數顯示、每一個參數當成總體
$@: 全部的參數顯示、每一個參數當成個體
$#: 顯示一共多少個參數
![1563512996454050.png image.png](http://static.javashuo.com/static/loading.gif)
![1563513003872702.png image.png](http://static.javashuo.com/static/loading.gif)
7、預約義變量
事先定義好的變量
$$:當前進程的id
$!:後臺的最後一個進程id
$?:最後一次執行的命令返回狀態,0爲正確執行。
mytest.sh中:
![1563513026236884.png image.png](http://static.javashuo.com/static/loading.gif)
mytest02.sh中:
![1563513039518690.png image.png](http://static.javashuo.com/static/loading.gif)
![1563513046519344.png image.png](http://static.javashuo.com/static/loading.gif)
8、變量運算符
1)表達式:變量= $[數字或者參數運算+、-、*、/等]
2)expr表達式:變量=`expr 表達式`
(運算符先後必須有空格、乘法\*)
![1563513128815745.png image.png](http://static.javashuo.com/static/loading.gif)
![1563513137313431.png image.png](http://static.javashuo.com/static/loading.gif)