变量常用子串
${#string} 返回$string长度
${string:position} 在$string中从位置positon之后开始提取子串
${string:position:length} 在$string中从位置positon之后开始提取长度length的子串
${string#substring} 从变量$string开关开始删除最短匹配的$substring
${string%substring} 从变量$string结尾开始删除最短匹配的$substring
${string/substring/replaces} 使用$replaces来代替第一匹配的$substring
${string/#substring/replaces} 如果$string前缀匹配$substring,就用$replaces来代替匹配$substring
${string/%substring/replaces} 如果$string后缀匹配$substring,就用$replaces来代替匹配$substring
变量替换
如果变量未定义,则返回默认值
${value:-word} 当变量未定义或值为空时,返回值为word内容,否则返回变量的值
${value-word} 与${value:-word} 类似
${value:=word} 如果变量名存在且非null,则返回变量值,否则设置这个变量值为word,并返回其值。
${value:+word] 如果变量名存在且非null,则返回word,否则返回null;用于测试变量是否存在。
${value?“not defined”} 如果变量存在且非null,则返回变量值,否则显示变量名:messages,并退出当前命令或脚本。
转载于:https://blog.51cto.com/seneagle/1730628