Shell的变量功能
变量分为:本地变量,环境变量,位置变量
(1)本地变量只在当前shell生效
[root@daniel ~]# echo $$ 25976 [root@daniel ~]# bash [root@daniel ~]# echo $$ 26020
export配置环境变量,对所有shell生效
[root@daniel ~]# xx=100 [root@daniel ~]# export xx [root@daniel ~]# echo $xx 100 [root@daniel ~]# bash [root@daniel ~]# echo $xx 100
通过直接赋值方式的变量在重启系统后将失效,可以通过修改配置文件的方式来永久有效。
通过修改隐藏文件.bash_profile文件
[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文件内容。
系统当前的环境变量有:
[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可以修改前面提示符
\u 当前登录用户 \h 当前用户home \W 当前工作目录相对路径 \w 当前工作目录绝对路径
[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
%从最后到最前开始匹配。