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脚本,其能完成的功能往往令 ...
随机推荐
- Linux下Apache服务器配置
Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm //主程序包 httpd-devel-2.2.3-29.e15.i ...
- REX-Ray 了解
REX-Ray 是一个 EMC {code} 团队领导的开源项目,为 Docker.Mesos 及其他容器运行环境提供持续的存储访问.其设计旨在囊括通用存储.虚拟化和云平台,提供高级的存储功能. 当前 ...
- CentOS下编译安装LNMP环境
一.卸载系统预安装的LAMP软件 rpm -qa|grep httpd rpm -e httpd httpd-tools rpm -qa|grep mysql rpm -e mysql mysql-l ...
- wordpress插件汉化包,和使用教程
点击下载汉化包 解压后上传到该插件的 languages 目录即可
- 洛谷【P4551】最长异或路径
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://www.luogu.org/problemnew/show ...
- VC编译选项 md /mdd /ml /mt/mtd
VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库 库文件Single threa ...
- Juicer自定义函数
首先,先写自定义的方法: function (sex) { ; ; var Range = Max - Min; var Rand = Math.random(); var res = (Min + ...
- SQL Server数据库常用的T-SQL命令
1. 查看数据库的版本 select @@version 2.查看数据库所在机器操作系统参数 exec master..xp_msver 3. 查看数据库启动的参数 sp_configure 4.查看 ...
- 阻塞队列之二:LinkedTransferQueue
一.LinkedTransferQueue简介 TransferQueue是一个继承了BlockingQueue的接口,并且增加若干新的方法.LinkedTransferQueue是TransferQ ...
- Python模块包(pycharm右键创建文件夹和python package的区别)中__init__.py文件的作用
在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很 ...