[bash's parameter expansion]

 #: find first from left, remove-left

 ##: find last from left, remove left

 %: find first from right, remove right

 %%: find last from right, remove right

 example 1:

 parameter     result
----------- ------------------------------
${NAME} polish.ostrich.racing.champion
${NAME#*.} ostrich.racing.champion
${NAME##*.} champion
${NAME%%.*} polish
${NAME%.*} polish.ostrich.racing

 example 2:

 parameter     result
----------- --------------------------------------------------------
${FILE} /usr/share/java-1.4.-sun/demo/applets/Clock/Clock.class
${FILE#*/} usr/share/java-1.4.-sun/demo/applets/Clock/Clock.class
${FILE##*/} Clock.class
${FILE%%/*}
${FILE%/*} /usr/share/java-1.4.2-sun/demo/applets/Clock

string slice:

${string::}   # The third character of string (, ,  = third)
${string:} # The string starting from the second character
# Note: this is equivalent to ${string#?}
${string%?} # The string with its last character removed.
${string: -} # The last character of string
${string:(-)} # The last character of string, alternate syntax
# Note: string:- means something entirely different; see below. ${file%.mp3} # The filename without the .mp3 extension
# Very useful in loops of the form: for file in *.mp3; do ...
${file%.*} # The filename without its last extension
${file%%.*} # The filename without all of its extensions
${file##*.} # The extension only, assuming there is one. If not, will expand to ${file}

toupper (^) and tolower (,):

 # string='hello, World!'
parameter result
----------- --------------------------------------------------------
${string^} Hello, World! # First character to uppercase
${string^^} HELLO, WORLD! # All characters to uppercase
${string,} hello, World! # First character to lowercase
${string,,} hello, world! # All characters to lowercase

assignment:

 ${var-word}             # if var is defined, use var; otherwise, "word"
${var+word} # if var is defined, use "word"; otherwise, nothing
${var=word} # if var is defined, use var; otherwise, use "word" AND...
# also assign "word" to var
${var?error} # if var is defined, use var; otherwise print "error" and exit

参考: http://bash.cumulonim.biz/BashFAQ(2f)073.html

bash's parameter expansion的更多相关文章

  1. bash shell parameter expansion

    1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...

  2. Linux Shell參数扩展(Parameter Expansion)

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

  3. 【转】bash 参数展开(Parameter Expansion)

    转自:http://www.360doc.com/content/13/0513/20/9437165_285204629.shtml ${parameter} 取parameter的值 ${para ...

  4. Linux Shell参数扩展(Parameter Expansion)

    Shell Command Language在线文档: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html ...

  5. [2012-04-25]shell大括号参数扩展(Parameter Expansion)

    参考了shell十三问 以及http://hi.baidu.com/leejun_2005/blog/item/ebfee11a4177ddc1ac6e751d.html 提炼下记忆方式: {% ca ...

  6. Shell parameter expansion

    使用sh写一些小型的脚本会使工作更加简单.有部分内容可能大家都比較陌生(至少我是这样). 就是变量有关的參数展开,以下就是一些简单的描写叙述和使用方法.能够使代码更加简洁 展开运算符 替换运算 ${v ...

  7. 再谈 $* 和 $@ 在 Bash 中的表现

    除非特别说明,本文中出现的 Shell 均指 Bash 4.3.首先说一个基础知识:Shell 中的变量在展开成值(Parameter Expansion)之后,这个值在某些上下文(Context)中 ...

  8. (转载)Bash 中的特殊字符大全

    转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...

  9. 学习bash

    工作8年,前6年基本是Windows环境下,也就是个鼠标党:两年前换工作开始用linux,也就开始了领略了命令行的强大,无论是直接在命令行组合命令,也还写几行简单的shell脚本,其能完成的功能往往令 ...

随机推荐

  1. [LOJ6261]一个人的高三楼

    loj description 给你一个长度为\(n\)的数列\(a_i\),求它的\(k\)次前缀和模\(998244353\).(就是做\(k\)次前缀和后的数列) \(n\le10^5,k\le ...

  2. apache phoenix 安装试用

    备注:   本次安装是在hbase docker 镜像的基础上配置的,主要是为了方便学习,而hbase搭建有觉得   有点费事,用镜像简单.   1. hbase 镜像 docker pull har ...

  3. REX-Ray 了解

    REX-Ray 是一个 EMC {code} 团队领导的开源项目,为 Docker.Mesos 及其他容器运行环境提供持续的存储访问.其设计旨在囊括通用存储.虚拟化和云平台,提供高级的存储功能. 当前 ...

  4. minio 安装以及使用

    1. 为了方便使用 docker 安装 docker run -p 9000:9000 minio/minio server /export   注意启动显示的 appkey secretkey  C ...

  5. vim中的加密文件

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sxhelijian/article/details/32911665 用vim编辑程序. 要连续输入 ...

  6. 解读ASP.NET 5 & MVC6 ---- 系列文章

    本系列的大部分内容来自于微软源码的阅读和网络,大部分测试代码都是基于VS RC版本进行测试的. 解读ASP.NET 5 & MVC6系列(1):ASP.NET 5简介 解读ASP.NET 5 ...

  7. Docker阿里云镜像加速器 for CentOS 7

    CentOS 7 CentOS使用配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/syst ...

  8. linux-docker安装

    https://yeasy.gitbooks.io/docker_practice/content/install/centos.html

  9. 在CentOS上把MySQL从5.5升级到5.6(转)

    http://www.th7.cn/db/mysql/201408/66064.shtml 在CentOS上把MySQL从5.5升级到5.6 摘要:本文记录了在CentOS 6.3上,把MySQL从5 ...

  10. Spark Streaming性能调优

    数据接收并行度调优(一) 通过网络接收数据时(比如Kafka.Flume),会将数据反序列化,并存储在Spark的内存中.如果数据接收称为系统的瓶颈,那么可以考虑并行化数据接收.每一个输入DStrea ...