本文主要參考: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. Spring Cloud练习1

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  2. python note of class

    reference: https://www.zhihu.com/question/27699413/answer/267906889 摘要: 我们在描述一个真实对象(物体)时包括两个方面:它可以做什 ...

  3. LG-P1311选择客栈

    题目 暴力十分好想但你写不出来qwq 正解十分好写但你想不出来qaq 我们先读题,发现k其实没什么用 同时暴力枚举两个客栈的话会超时,所以只能同时枚举一个.我们枚举第二个客栈,然后用第二个客栈反推出前 ...

  4. [Luogu] P3846 [TJOI2007]可爱的质数

    题目描述 给定一个质数P(2<=P<2312^{31}231),以及一个整数B(2<=B<P),一个整数N(2<=N<P). 现在要求你计算一个最小的L,满足BL≡ ...

  5. buf.values()

    buf.values() 返回:{Iterator} 创建并返回一个包含 Buffer 值(字节)的迭代器.当 Buffer 使用 for..of 声明时将自动调用该函数. const buf = B ...

  6. 89-Relative Vigor Index 相对活力指数指标.(2015.7.4)

    Relative Vigor Index 相对活力指数指标 ~计算: RVI = (CLOSE-OPEN)/(HIGH-LOW) RVIsig=SMA(RVI,N) ~思想: 牛市中,收盘>开盘 ...

  7. Oracle创建 表空间 用户 给用户授权命令

    //创建表空间 create tablespace ACHARTSdatafile 'D:\oradata\orcl\ACHARTS01.DBF' size 800mautoextend on nex ...

  8. 定义一个复数类Complex

    #include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex( ...

  9. centos相关

    查看虚拟机里的Centos7的IP:ip addr或者ifconfig  ---https://blog.csdn.net/dancheren/article/details/73611878 Cen ...

  10. 详解js变量声明提升

    之前一直觉会认为javascript代码执行是由上到下一行行执行的.自从看了<你不知道的JS>后发现这个观点并不完全正确.先来给大家举一个书本上的的例子: var a='hello wor ...