[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. RBAC (基于角色的访问控制)

    基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而 ...

  2. new/delete和malloc/free区别

    相同点: 都可用于申请动态内存和释放内存 不同点: 操作对象有所不同. 本质区别: malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符,对象在创建的同时要自动 ...

  3. ABP学习之路

    ABP文档介绍 http://www.cnblogs.com/kid1412/p/AbpDocumentContent.html [ABP开源项目]--vue+vuex+vue-router+EF的权 ...

  4. Request.UrlReferrer详解

    使用前需要进行判断: if (Request != null && Request.UrlReferrer != null && Request.UrlReferrer ...

  5. 从内存的角度观察 堆、栈、全局区(静态区)(static)、文字常量区、程序代码区

    之前写了一篇堆栈的,这里再补充下内存其他的区域 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) — 一般由程 ...

  6. 试玩swoole扩展 第一天

    安装 pecl install swoole 部分过程: configure: creating ./config.statusconfig.status: creating config.hrunn ...

  7. 老齐python-基础2(字符串)

    1.字符串 1.1索引和切片 索引: >>> lang = "study python" >>> lang[0] 's' >>> ...

  8. zabbix 布署实践【8 监控windows server】

    参考http://www.cnblogs.com/likehua/p/3968689.html的思路,我安装的是zabbix 3.0 从zabbix官网下载windown的 Zabbix pre-co ...

  9. HttpClient使用示例

    1)使用HttpClient发送GET请求 public class MainActivity extends Activity implements OnClickListener { privat ...

  10. rtmp发送H264及aac的音视频

    RTMP推送的音视频流的封装形式和FLV格式相似,由此可知,向FMS推送H264和AAC直播流,需要首先发送"AVC sequence header"和"AAC sequ ...