bash's parameter expansion
[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的更多相关文章
- bash shell parameter expansion
1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...
- Linux Shell參数扩展(Parameter Expansion)
本文主要參考:http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 其它资料:ht ...
- 【转】bash 参数展开(Parameter Expansion)
转自:http://www.360doc.com/content/13/0513/20/9437165_285204629.shtml ${parameter} 取parameter的值 ${para ...
- Linux Shell参数扩展(Parameter Expansion)
Shell Command Language在线文档: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html ...
- [2012-04-25]shell大括号参数扩展(Parameter Expansion)
参考了shell十三问 以及http://hi.baidu.com/leejun_2005/blog/item/ebfee11a4177ddc1ac6e751d.html 提炼下记忆方式: {% ca ...
- Shell parameter expansion
使用sh写一些小型的脚本会使工作更加简单.有部分内容可能大家都比較陌生(至少我是这样). 就是变量有关的參数展开,以下就是一些简单的描写叙述和使用方法.能够使代码更加简洁 展开运算符 替换运算 ${v ...
- 再谈 $* 和 $@ 在 Bash 中的表现
除非特别说明,本文中出现的 Shell 均指 Bash 4.3.首先说一个基础知识:Shell 中的变量在展开成值(Parameter Expansion)之后,这个值在某些上下文(Context)中 ...
- (转载)Bash 中的特殊字符大全
转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...
- 学习bash
工作8年,前6年基本是Windows环境下,也就是个鼠标党:两年前换工作开始用linux,也就开始了领略了命令行的强大,无论是直接在命令行组合命令,也还写几行简单的shell脚本,其能完成的功能往往令 ...
随机推荐
- [BZOJ5338][TJOI2018]xor
bzoj luogu descirption 现在有一棵以 \(1\) 为根节点的由 \(n\) 个节点组成的树,树上每个节点上都有一个权值 \(v_i\) .现在有 \(Q\) 次操作,操作如下: ...
- eclipse adt调试出错,不能产出apk问题
The connection to adb is down, and a severe error has occured http://blog.csdn.net/h7870181/article/ ...
- Quick 3.3 的代码资源加密
http://cn.cocos2d-x.org/tutorial/show?id=1507 http://cn.cocos2d-x.org/tutorial/show?id=1447 http://b ...
- Java API 操作 Mongodb
本次测试环境使用一台ip为 192.168.2.23 的虚拟机 一.依赖 <dependency> <groupId>org.mongodb</groupId> & ...
- 老齐python-基础7(文件操作、迭代)
在python3中,没有file这个内建类型了(python2中,file是默认类型) 1.读文件 创建文件,130.txt 并在里面输入 learn python http://qiwsir.git ...
- vs2015安装ORACLE的DbFirst
不说DbFirst好在哪里,它和ModelFirst,CodeFirst都各有各的好,由于对于已经存在的一个大型的业务库,使用EntityFramework的更倾向于DbFirst,因为好多同事已经习 ...
- [欢乐赛]班服 状压DP
班服 (shirt.pas/.c/.cpp) 时间限制:1s:内存限制 128MB 题目描述: 要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100.现在每个班都挑出了一些样 ...
- windows10 vs2015编译 带nginx-rtmp-module 模块的32位nginx
1 下载必要软件 从 http://xhmikosr.1f0.de/tools/msys/下载msys:http://xhmikosr.1f0.de/tools/msys/MSYS_MinGW-w6 ...
- spring cache之自定义keys的过期时间
spring @cacheable注解默认不支持方法级别的缓存失效时间,只能通过配置来配置全局的失效时间 如果需要实现对方法级别的缓存支持失效时间机制,有一种比较简单的方法,spring配置文件如下: ...
- 在centos7上搭建mongodb副本集
1.安装副本集介绍 副本集(Replica Set)是一组MongoDB实例组成的集群,由一个主(Primary)服务器和多个备份(Secondary)服务器构成.通过Replication,将数据的 ...