Shell的變量功能shell
變量分爲:本地變量,環境變量,位置變量
vim
(1)本地變量只在當前shell生效bash
[root@daniel ~]# echo $$ 25976 [root@daniel ~]# bash [root@daniel ~]# echo $$ 26020
export配置環境變量,對全部shell生效ide
[root@daniel ~]# xx=100 [root@daniel ~]# export xx [root@daniel ~]# echo $xx 100 [root@daniel ~]# bash [root@daniel ~]# echo $xx 100
經過直接賦值方式的變量在重啓系統後將失效,能夠經過修改配置文件的方式來永久有效。ci
經過修改隱藏文件.bash_profile文件
qt
[root@daniel ~]# vim .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin xx=100 export PATH export xx [root@daniel ~]# echo $xx 100
上面實例只對當前用戶生效,如需對全部用戶生效,需修改/etc/bashrc文件內容。
it
系統當前的環境變量有:io
[root@daniel ~]# echo $HOME /root [root@daniel ~]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@daniel ~]# echo $PS1 [\u@\h \W]\$
修改$PS1能夠修改前面提示符
function
\u 當前登陸用戶 \h 當前用戶home \W 當前工做目錄相對路徑 \w 當前工做目錄絕對路徑class
[root@daniel ~]# export PS1="[\u@\h \w]$"
位置變量
[root@daniel ~]# set 1 2 3 4 5 6 7 8 9 a b c d e f [root@daniel ~]# echo $6 6 [root@daniel ~]# echo $10 10 [root@daniel ~]# echo ${10} a
單引號,雙引號,反引號
[root@daniel ~]# aa=100 [root@daniel ~]# echo '$aa' $aa [root@daniel ~]# echo "$aa" 100 [root@daniel ~]# echo `$aa` -bash: 100: command not found [root@daniel ~]# echo `aa` -bash: aa: command not found [root@daniel ~]# echo `uname -r` 2.6.32-431.el6.x86_64
$?表明上一次命令返回的值,0表示執行成功,其餘數字表明失敗。
[root@daniel ~]# echo `uname -r` 2.6.32-431.el6.x86_64 [root@daniel ~]# echo $? 0 [root@daniel ~]# fds -bash: fds: command not found [root@daniel ~]# echo $? 127
對數值,字符進行比較,-eq -gt -lt 比較數字 ,= > <比較字符
[root@daniel ~]# [ 3 -eq 2 ]; echo $? 1 [root@daniel ~]# [ 3 -lt 2 ]; echo $? 1 [root@daniel ~]# [ 3 -gt 2 ]; echo $? 0 [root@daniel ~]# [ abc > a ]; echo $? 0
declare -r aa 只讀變量
[root@daniel ~]# unset xx [root@daniel ~]# r=${xx-"abcdef"} [root@daniel ~]# echo $r abcdef [root@daniel ~]# xx=100 [root@daniel ~]# r=${xx-"abcdef"} [root@daniel ~]# echo $r 100 [root@daniel ~]# [root@daniel ~]# unset xx [root@daniel ~]# r=${xx="abcdef"} [root@daniel ~]# echo $r abcdef [root@daniel ~]# xx=200 [root@daniel ~]# r=${xx="abcdef"} [root@daniel ~]# echo $r 200 [root@daniel ~]# unset xx [root@daniel ~]# r=${xx:="adcb"} [root@daniel ~]# echo $r adcb [root@daniel ~]# echo $xx adcb [root@daniel ~]# xx= [root@daniel ~]# r=${xx:="adcb"} [root@daniel ~]# echo $r adcb [root@daniel ~]# echo $xx adcb [root@daniel ~]# xx=300 [root@daniel ~]# r=${xx:="adcb"} [root@daniel ~]# echo $r 300 [root@daniel ~]# echo $xx 300 [root@daniel ~]# unset xx [root@daniel ~]# r=${xx:-"bdca"} [root@daniel ~]# echo $r bdca [root@daniel ~]# echo $xx [root@daniel ~]# xx= [root@daniel ~]# r=${xx:-"bdca"} [root@daniel ~]# echo $r bdca [root@daniel ~]# xx=500 [root@daniel ~]# r=${xx:-"bdca"} [root@daniel ~]# echo $r 500 [root@daniel ~]# unset xx [root@daniel ~]# r=${xx:?"adbce"} -bash: xx: adbce [root@daniel ~]# xx= [root@daniel ~]# r=${xx:?"adbce"} -bash: xx: adbce [root@daniel ~]# xx=600 [root@daniel ~]# r=${xx:?"adbce"} [root@daniel ~]# echo $r 600 [root@daniel ~]# echo $xx 600 [root@daniel ~]# xx=10 [root@daniel ~]# r=${xx:+"dfgl"} [root@daniel ~]# echo $r dfgl [root@daniel ~]# unset xx [root@daniel ~]# r=${xx:+"dfgl"} [root@daniel ~]# echo $r
變量內容的刪除替代與替換
[root@daniel ~]# echo $path /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@daniel ~]# r=${path#*bin} [root@daniel ~]# echo $r :/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@daniel ~]# r=${path##*bin} [root@daniel ~]# echo $r
兩個##從最遠的刪到開頭,因此全部的bin都將刪除。
[root@daniel ~]# r=${path%*bin} [root@daniel ~]# echo $r /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/ [root@daniel ~]# r=${path%%*bin} [root@daniel ~]# echo $r
%從最後到最前開始匹配。