shell 变量的赋值

shell变量设置与显示

[root@localhost ~]# echo ${MAIL}
/var/spool/mail/root
[root@localhost ~]# echo ${MAIL##/*/} #贪婪前匹配删除
root
[root@localhost ~]# echo ${MAIL#/*/} #前匹配删除
spool/mail/root
[root@localhost ~]# echo ${MAIL%/*} #后匹配删除
/var/spool/mail
[root@localhost ~]# echo ${MAIL%%/*} #贪婪后匹配删除

[root@localhost ~]# echo ${MAIL//oo/aa} #全局替换
/var/spaal/mail/raat
[root@localhost ~]# echo ${MAIL/oo/aa} #仅替换第一个
/var/spaal/mail/root


[root@localhost ~]# unset a;var=${a-b};echo $a,$var # var = (a == null) ? a : b
,b
[root@localhost ~]# a='foo';var=${a-b};echo $a,$var
foo,foo
[root@localhost ~]# unset a;var=${a=b};echo $a,$var # var = a= (a == null)? a : b
b,b
[root@localhost ~]# a='foo';var=${a=b};echo $a,$var
foo,foo

变量设置方式 str没有设置 str为空字符 str已设置为非空字符
var=${str-expr} var=expr var= var=$str
var=${str:-expr} var=expr var=expr var=$str
var=${str+expr} var=expr var=expr var=expr
var=${str:+expr} var=expr var= var=expr
var=${str=expr}

var=expr
str=expr

str不变
var=
str不变
var=$str
var=${str:=expr}

var=expr
str=expr

var=expr
str=expr
str不变
var=$str
var=${str?expr} expr输出至stderr var= var=$str
var=${str:?expr} expr输出至stderr expr输出至stderr var=$str

你可能感兴趣的:(shell编程)