假设定义了一个变量为,代码如下:

file=/dir1/dir2/dir3/my.file.txt

可以用${ }分别替换得到不同的值:


${file#*/}:     删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt 非贪婪匹配
${file##*/}:    删掉最后一个 / 及其左边的字符串:my.file.txt 贪婪匹配
${file#*.}:       删掉第一个 . 及其左边的字符串:file.txt
${file##*.}:    删掉最后一个 . 及其左边的字符串:txt
${file%/*}:     删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:    删掉第一个 / 及其右边的字符串:(空值)
${file%.*}:     删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:     删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my

 

记忆的方法为:

#   是 去掉左边(键盘上#在 $ 的左边)
%  是去掉右边(键盘上% 在$ 的右边)
单一符号是非贪婪匹配;两个符号是贪婪匹配

${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2

也可以对变量值里的字符串作替换:
${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt

[root@localhost ~]# p=123abc
[root@localhost ~]# echo ${p//[0-9]/}      #将变量中的数字替换为空
abc

+----------------------------------------------------------------------+
|Form Meaning
+----------------------------------------------------------------------+
|${variable:?word} Complain if undefined or null
|${variable:-word} Use new value if undefined or null
|${variable:+word} Opposite of the above
|${variable:=word} Use new value if undefined or null, and redefine.
+----------------------------------------------------------------------+

foo=${bar:-something}

echo $foo # something
echo $bar # no assignement to bar, bar is still empty foo=${bar:=something} echo $foo # something
echo $bar # something too, as there's an assignement to bar

man bash :

${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise,
the value of parameter is substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter.
The value of parameter is then substituted. Positional parameters and special parameters may not be
assigned to in this way.
${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to
that effect if word is not present) is written to the standard error and the shell, if it is not inter-
active, exits. Otherwise, the value of parameter is substituted.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of
word is substituted.

Shell中的${}、##和%%使用范例的更多相关文章

  1. 介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  2. shell 中的特殊符号的含义

    来源:http://blog.sina.com.cn/s/blog_62a151be0100x9rn.html 第四章 基本功 - 特殊符号 学习撰写 script 最迅速的捷径是观摩别人的 scri ...

  3. shell 中的for、while循环及if语句

    shell与其他语言一样也支持for.while循环 for循环的一般格式如下: #!/bin/sh for 变量 in 列表 do command command command ......... ...

  4. shell中脚本调试----学习

    1.使用dos2unix命令处理在windows下开发的脚本 将windows下编辑的脚本放置到linux下执行的情况如下: [root@ks ~]# cat -v nginx.sh #!/bin/b ...

  5. shell中#*,##*,#*,##*,% *,%% *的含义及用法

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  6. 转载:shell中#*,##*,#*,##*,% *,%% *的含义及用法

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  7. linux shell 中的sleep命令

    开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...

  8. shell 中的与、或表达式

    今天总结一下linux shell中逻辑关机表达方式.逻辑与的表达: 1).if [ $xxx=a -a $xx=b ] 注:-a表示and的意思 2).if [ $xxx=a ] && ...

  9. shell简单用法笔记(shell中数值运算)二

    shell中变量值,如果不手动指定类型,默认都是字符串类型: 例如: a= b= c=$a+#b echo $c 结果会输出:123+456 shell中,如果要进行数值运算,可以通过一下方法: 方法 ...

  10. shell中{}的妙用

    shell中${}的妙用   1. 截断功能 ${file#*/}:       拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file##*/}:    拿 ...

随机推荐

  1. Perl学习笔记(3)----遍历哈希表的一个容易疏忽的地方

    今天做 Google的 Code Jam 上的一道题目:https://code.google.com/codejam/contest/351101/dashboard#s=p2,用Perl语言解答的 ...

  2. 04_dubbo_ioc

    [dubbo的IOC实现方法] dubbo的IOC具体实现在:T injectExtension( T instance )方法中,该方法在3个地方被使用: ExtensionLoader.getEx ...

  3. 139.00.007 Git学习-Cheat Sheet

    @(139 - Environment Settings | 环境配置) Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了. 友情附赠国外网 ...

  4. qt 样式表基本用法

    Qt样式表 QT样式表参考CSS层叠样式表设计,不同之处在于QT样式表应用于Widget世界. 可以使用QApplication::setStyleSheet()函数设置到整个应用程序上,也可以使用Q ...

  5. 'weblogic.kernel.Default (self-tuning) 问题weblogic层面解决办法

    声明:出现这个问题有程序方面.网络方面.weblogic设置方面等等原因,此文章主要讲述由于weblogic设置而导致的解决办法. 因为: 1.程序问题,需要项目自己去解决,weblogic在做优化处 ...

  6. C#接收WebSphere MQ中文乱码问题

    最近在用IBM的WebSphere MQ做数据传输,客户端操作系统Windows,服务器是AIX,我们用的都是UTF-8编码,但是联调时发现带中文的都出现了乱码.折腾了好久才发现问题,原来是CCSID ...

  7. cc.out

    Server: , win: 20pkt, SRU: 256KB, link_buf: 32pkt, Seed: , Block_trans: 1350200B, RTT: 100us, RTT_ra ...

  8. mongodb 3.4复制集详解

    1关闭数据库,打开三个mongodb数据库数据库实例 rs.printReplicationInfo() 2:原理 主库能够进行读写操作,一个复制集群只能有一个活跃的主库 一般情况下复制可以分为好几种 ...

  9. jbd2/dm-2-8 io太高

    用iotop查看发现[jbd2/dm-2-8]几乎占用了99%的io使用率,但是却没有输入输出 后来上网查找,网上有人说是个内核bug,需要升级内核,或者降低jdb2的提交次数,即重新挂载磁盘 添加c ...

  10. (名词 形容词 动词 副词)重读&(冠词 介词 连词 代词 辅助词(Be))弱读

    二,一些发音规则 除了上面的练习之外,这里还有几个注意点需要我们有足够的认识,那就是英语有重读.弱读.连读.爆破.语感(节奏和断句)等(其实当你跟读并背诵新概念之后,这一切都是神马,你不知觉地也会发现 ...