本文主要參考:http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

其它资料:http://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html

參数扩展的表示形式为:${expression}。

expression包含各种字符直到匹配上'}'。当出现下面情况时候'}'不会被检查来匹配:

1)在转义字符\之后,如\{;

2)在引號里面,如‘}’;

3) 在算术表达式,命令替换或者变量扩展里面的。如${value}

最简单的參数扩展形式如:${parameter}

使用例如以下模式能够改动參数扩展:

${parameter:-[word]}Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise,
the value of parameter shall be substituted.当${parameter}值为空或者没有设定的时候,用[word]值来替换。否则它就是该表达式的值。

[hdfs@cdh51kdc ~]$ bb=3
[hdfs@cdh51kdc ~]$ echo ${aa} [hdfs@cdh51kdc ~]$ echo ${bb}
3
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
3
[hdfs@cdh51kdc ~]$ aa=2
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
2

${parameter:=[word]}Assign Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be assigned to parameter.
In all cases, the final value of parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.当${parameter}值为空或者没有设定的时候。用[word]值来给${parameter}赋值并替换最后的表达式。否则它就是该表达式的值。

[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}
2
[hdfs@cdh51kdc ~]$ echo ${aa:=${bb}}
2
[hdfs@cdh51kdc ~]$ echo ${cc} [hdfs@cdh51kdc ~]$ echo ${cc:=${bb}}
3
[hdfs@cdh51kdc ~]$ echo ${cc}
3

${parameter:?

[word]}Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted)
shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall be substituted. An interactive shell need not exit.当${parameter}值为空或者没有设定的时候,用[word]值作为标准错误输出提示并退出shell且返回非0状态。

否则它就是该表达式的值。

[hdfs@cdh51kdc ~]$ echo ${cc:?"Value not set"}
3
[hdfs@cdh51kdc ~]$ echo ${dd:? "Value not set"}
-bash: dd: Value not set

${parameter:+[word]}Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word (or an empty string if word is
omitted) shall be substituted.当${parameter}值为空或者没有设定的时候,表达式返回null。否则用[word]替换表达式的值。

[hdfs@cdh51kdc ~]$ echo ${cc:+"Value not set"}
Value not set
[hdfs@cdh51kdc ~]$ echo ${dd:+"Value not set"}

${#parameter}

String Length. The length in characters of the value of parameter shall be substituted. If parameter is '*' or '@', the result of
the expansion is unspecified. If parameter is unset and set -u is in
effect, the expansion shall fail.表达式返回${parameter}值中字符的个数。

[hdfs@cdh51kdc ~]$ echo ${#cc}
1
[hdfs@cdh51kdc ~]$ echo ${#dd}
0

The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation (see Pattern
Matching Notation
), rather than regular expression notation, shall be used to evaluate the patterns. If parameter is '#''*', or '@', the result of the expansion is unspecified. If parameter is unset and set -u is
in effect, the expansion shall fail. Enclosing the full parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted, whereas quoting characters within the braces shall have this effect. In each
variety, if word is omitted, the empty pattern shall be used.

${parameter%[word]}

Remove Smallest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the suffix matched by the pattern deleted.
If present,word shall not begin with an unquoted '%'.

用[word]产生的模式来匹配${parameter}的后缀并去除掉最小匹配部分

[hdfs@cdh51kdc ~]$ echo ${cc}
Value not set
[hdfs@cdh51kdc ~]$ echo ${cc%"et"}
Value not s

${parameter%%[word]}Remove Largest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest
portion of the suffix matched by the pattern deleted.用[word]产生的模式来匹配${parameter}的后缀并去除掉最大匹配部分

[hdfs@cdh51kdc ~]$ echo ${cc%%t*}
Value no
[hdfs@cdh51kdc ~]$ echo ${cc%t*}
Value not se

${parameter#[word]}Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest
portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted '#'.${parameter##[word]}Remove Largest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest
portion of the prefix matched by the pattern deleted.最后两个各自是去除掉前缀的[word]最小匹配和最大匹配

[hdfs@cdh51kdc ~]$ echo ${cc#*t}
set
[hdfs@cdh51kdc ~]$ echo ${cc##*t} [hdfs@cdh51kdc ~]$ echo ${cc#V}
alue not set

最新文章

  1. yii2实战教程之第一个Yii程序
  2. Intellij IDEA中的Mybatis Plugin破解
  3. Eclipse+CDT+GDB调试android NDK程序(转)
  4. Angularjs,WebAPI 搭建一个简易权限管理系统 —— WebAPI项目主体结构(四)
  5. 论文笔记之:Playing Atari with Deep Reinforcement Learning
  6. 【linux】linux如何进入单人维护模式修改root密码
  7. poj 2728 最优比率生成树
  8. Memcached源码分析——hash
  9. CI框架中遇见的一些错误和解决方法 笔记
  10. EF 下的code fist 模式编程
  11. AutoCAD.net支持后台线程-Socket服务端
  12. 【Unity Shaders】ShadowGun系列之二——雾和体积光
  13. Kubernetes 弹性伸缩全场景解析 (四)- 让核心组件充满弹性
  14. Python练习-列表生成式-2018.11.30
  15. mac sierra 10.12部分注册机Special-K+CORE Keygen不能运行的问题
  16. exe4j打包java应用程序
  17. 【bzoj2789】 Letters 树状数组
  18. MsSQL使用加密连接SSL/TLS
  19. Java 连接MS Access数据库
  20. ORACLE联机日志文件丢失或损坏的处理方法(转)

热门文章

  1. 使用python划分数据集
  2. make、makefile
  3. Python框架Django的入门
  4. CSU1011: Counting Pixels
  5. win10 专业版 安装tornado 的步骤
  6. laravel学习笔记1--基础
  7. Go:二分查找
  8. c++基础_01字串
  9. leetcode-88合并两个有序数组
  10. Matplotlib中的颜色