1、未设置就临时替换(:-)
冒号:用来检验变量是否设置过,如果没有冒号,则认为设置过,不替换
$fruit=peach
$echo ${fruit:-plum}
peach
$fruit=
$echo ${fruit:-plum}
plum
$echo $fruit
$
2、未设置就永久替换(:=)
$name=
$echo ${name:=Peter}
Peter
$echo $name
Peter
3、设置了就临时替换(:+)
$foo=grapes
$echo ${foo:+pears}
pears
$echo $foo
grapes
4、未设置就打印消息(:?)
$echo ${namex:?"namex is undefined"}
namex: namex is undefined
$echo name=${1:?"requires an argument"} //适用于脚本中检测位置参量
1: requires an argument
echo ${y?}
y: parameter null or not set //打印默认消息
5、子字符串(:m:n)
$var=notebook
$echo ${var:0}
notebook
$echo ${var:0:4}
note
$echo ${var:4:4}
book
时间: 2024-10-10 03:22:20