shell变量------变量替换
每个运算符内的冒号都是可选的,如果省略冒号,则将没定定义中的“存在且非null”部分改为“存在”,也就是说,运算符仅用于测试变量是否存在。
当变量未定义或者返回值为空时,返回值为word内容,否则返回变量的值。 [[email?protected] yusheng]# result=${test:-UNSET} [[email?protected] yusheng]# echo $result UNSET [[email?protected] yusheng]# echo $test #====>这里是空 结论:当test变量没有内容时,就返回了后面的UNSET 结论:当test变量有内容时,就返回了test变量的内容
[[email?protected] yusheng]# result=${test:=UNSET} [[email?protected] yusheng]# echo $result UNSET [[email?protected] yusheng]# echo $test UNSET #提示:变量不存在时,会给变量赋值后面的内容,确保变量始终有值
[[email?protected] yusheng]# echo ${value:?"not defined"} -bash: value: not defined [[email?protected] yusheng]# value=1 [[email?protected] yusheng]# echo ${value:?"not defined"} 1 [[email?protected] yusheng]# unset value [[email?protected] yusheng]# echo ${value:?"not defined"} -bash: value: not defined #提示:用于捕捉由于变量未定义而导致的错误,如:"not defined"
[[email?protected] yusheng]# result=${value:+1} [[email?protected] yusheng]# echo $result [[email?protected] yusheng]# value="handsome boy" [[email?protected] yusheng]# result=${value:+1} [[email?protected] yusheng]# echo $result 1 #提示:用于测试变量是否存在 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |