1.${variable:-word}

${variable:-word} 如果variable已经被设置了,且不为空,则代入它的值,否则代入word;

$ fruit=peach

$ echo ${fruit:-plum}

peach

$ echo ${newfruit:-apple}

apple

 

2.
${variable:=word}

${variable:=word}如果variable已经被设置且不为空,则代入它的值,否则代入word,并且在后面variable始终为word的值。位置参量不能用这种方式赋值。

$ name=

$ echo ${name:=peter}

peter

$ echo $name

peter

 3.${variable:+word} 

${variable:+word} 如果变量variable已被设置且值为非空,代入word值,否则什么也不代入,

$ foo=grapes

$ echo ${foo:+pears}

pears

$ echo $foo

grapes

4. ${variable:?word} 

${variable:?word} 如果变量variable已被设置值且为非空,就代入它的值,否则输出word并从shell中退出,如果有省略了word,就会显示信息:parameter null or not set.

$ echo ${namex:?"namex is undefined"}

namex: namex is undefined

5.$ echo ${y?]

$ echo ${y?]

y: parameter null or not set

6.${variable:offset}

${variable:offset} 获得变量从variable值中位置从offset开始的子串,偏移为从0到串尾。

${variable:offset:length} 获得variable值位置从offset开始长度为length的子串。

$var=notebook

$ echo ${var:0:4}

note

$ echo ${var:4:4]

book

05-11 22:06