首页 > 代码库 > Shell parameter expansion

Shell parameter expansion

使用sh写一些小型的脚本会使工作更加简单,有部分内容可能大家都比较陌生(至少我是这样),

就是变量有关的参数展开,下面就是一些简单的描述和用法,可以使代码更加简洁

展开运算符

替换运算

${varname:-word} var exist & not null,return value ,else return word

${varname:=word}  var exist & not null,return value, else varname=word & return $varname

${varname:?message} var exist & not null,return value, else show message & if not define exit


#test variable is or not exist

${varname:+word} if exist & not null ,return word, else return null


(:)is optional


匹配运算

${variable#pattern}

${variable##pattern}

${variable%pattern}

${variable%%pattern}

# % is from left or right