本文主要參考: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

Linux Shell參数扩展(Parameter Expansion)的更多相关文章

  1. ubuntu nginx安装及相关linux性能參数优化

    一.安装 下载源代码,解压:tar -xzvf nginx-1.4.7.tar.gz ./configure make && make install 改动默认nginx的监听port ...

  2. Linux shell猜数游戏

    题目:猜随机数随机1-100中的一个数字,要求用户猜数字,猜中则退出脚本并告知用户猜测次 数和随机数字,否则要求用户继续猜,并告知当前猜的数字和随机数的关系. #!/bin/bash #猜数游戏 Ra ...

  3. AFNetworking 3.0携带參数上传文件Demo

    一.服务端代码: 服务端是java用国产nutz搞的,实际mvc框架都大同小异.就是提交文件的同一时候还带了个表单參数 @AdaptBy(type=UploadAdaptor.class, args= ...

  4. linux下形如{command,parameter,parameter}执行命令 / bash花括号扩展

    背景 在复现vulhub上的漏洞ActiveMQ Deserialization Vulnerability (CVE-2015-5254)时,发现官方文档给出反弹shell的payload bash ...

  5. linux kernel的cmdline參数解析原理分析

    利用工作之便,今天研究了kernel下cmdline參数解析过程.记录在此.与大家共享.转载请注明出处.谢谢. Kernel 版本:3.4.55 Kernel启动时会解析cmdline,然后依据这些參 ...

  6. 【原】用PHP搭建基于swoole扩展的socket服务(附PHP扩展的安装步骤及Linux/shell在线手册)

    最近公司的一项目中,需要用PHP搭建一个socket服务. 本来PHP是不适合做服务的,因为和第三方合作,需要采用高效而稳定的TCP协议进行数据通信.经过多次尝试,最终选择了开源的PHP扩展:swoo ...

  7. Linux Shell多进程并发以及并发数控制

    1. 基础知识准备 1.1. linux后台进程 Unix是一个多任务系统,允许多用户同时运行多个程序.shell的元字符&提供了在后台运行不需要键盘输入的程序的方法.输入命令后,其后紧跟&a ...

  8. mysqladmin在SuSE linux系统中--sleep參数使用不准确问题

    我们都知道,在MySQL中.能够使用mysqladmin命令的extended-status选项来查看MySQL的执行状态,比方获取我们经常关注的几个值: # mysqladmin -uroot -p ...

  9. shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟

    /*********************************************************************  * Author  : Samson  * Date   ...

随机推荐

  1. docker 容器挂载主机目录,访问出现 cannot open directory /mnt/home/webroot/: Permission denied 的解决办法

    问题原因及解决办法 原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题: 1.在运行容器的时候,给容器加特权,及加上 --privileged= ...

  2. 签名mobileconfig 重签ipa

    .mobileconfig文件的签名 生成Apache专用的三个证书 双击[2331135_zgp.ink_chain.crt]可以看到它是个根证书,在mac上,双击它,导入到证书中心,然后再导出为p ...

  3. 第2节 mapreduce深入学习:12、reducetask运行机制(多看几遍)

    ReduceTask的运行的整个过程 背下来1.启动线程到mapTask那里去拷贝数据,拉取属于每一个reducetask自己内部的数据2.数据的合并,拉取过来的数据进行合并,合并的过程,有可能在内存 ...

  4. HYSBZ - 2763 飞行路线(分层图最短路线)

    题目: Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价 ...

  5. 集训第六周 数学概念与方法 J题 数论,质因数分解

    Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all g ...

  6. <mongoose>……find与findOne的区别……//

    mongoose中的 find 和 findOne 都是用来查找指定表的数据的 find指的是查找指定表的所有数据,返回的是数组 User.find().then((result)=>{ con ...

  7. xcap发包工具的简单使用3(报文描述)

    之前详细介绍了如何构造以及发送报文,现在简单对报文描述一下 1.Ethernet MAC:填写报文目的mac和源mac地址 Vlan:支持单vlan,QinQ,如果有更多的vlan,请填写在“More ...

  8. 【51NOD1766】树上的最远点对(线段树,LCA,RMQ)

    题意:n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间, 表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c< ...

  9. 【HDOJ6118】度度熊的交易计划(费用流)

    题意: 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个 ...

  10. cogs——1364. 聚会

    1364. 聚会 ★   输入文件:partyb.in   输出文件:partyb.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 小S想要从某地出发去同学k的家中参加 ...