变量设置方式说明示例
${#变量}变量的长度A='Hello world!'

echo ${#A}

结果:12
${变量:-word}如果变量为空或未定义,则变量展开为”word”;否则,展开为变量的值

A='Hello world!'

echo ${A:-OK}

结果:Hello wold!

unset A或 A=''

echo ${A:-OK}

结果:OK
${变量:?word}如果变量为空或未定义,则命令退出,命令返回值为1

A='Hello world!'

echo ${A:?OK}

结果:Hello world!

unset A或 A=''

echo ${A:?OK}

结果:-bash: A: OK

echo $?

结果:1

${变量:+word}如果变量为空或未定义,则不做任何操作;否则,展开为”word”值

A='Hello world!'

echo ${A:+OK}

结果:OK

unset A 或A=''

echo ${A:+OK}

结果:
${变量:=word}如果变量为空或未定义,则变量为”word”,且将”word”赋值给变量

A='Hello world!'

echo ${A:=OK}

结果:Hello world!

unset A 或A=''

echo ${A:=OK}

结果:OK

echo $A

结果:OK
${变量:偏移量}取从偏移量后一位开始到末尾

A='Hello world!'

echo ${A:3}

结果:Hello world!
${变量:偏移量:取长n}取从偏移量后一位开始取n个字符

A='Hello world!'

echo ${A:3:4}

结果:Hello world!