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

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. hive中的bucket table

    前言 bucket table(桶表)是对数据进行哈希取值,然后放到不同文件中存储 应用场景 当数据量比较大,我们需要更快的完成任务,多个map和reduce进程是唯一的选择.但是如果输入文件是一个的 ...

  2. 大V云集!参加首届阿里巴巴在线技术峰会的八大理由

    由阿里巴巴集团.阿里巴巴技术协会(ATA)和阿里云云栖社区联合举办的首届阿里巴巴在线技术峰会(Alibaba Online Technology Summit)将于7月19日-21日20:00-21: ...

  3. Android 保存和恢复activity的状态数据

    一般来说, 调用onPause()和onStop()方法后的activity实例仍然存在于内存中, activity的所有信息和状态数据不会消失, 当activity重新回到前台之后, 所有的改变都会 ...

  4. 21.拉取&删除远程分支

    拉取 当 git fetch 命令从服务器上抓取本地没有的数据时,它并不会修改工作目录中的内容. 它只会获取数据然后让你自己合并. 然而,有一个命令叫作 git pull 在大多数情况下它的含义是一个 ...

  5. Android学习——Service(二)

    今天来介绍Service的第二种使用方式,Bind方式 Bind方式启动服务 Bind方式和Start方式启动很类似,都是通过Intent来启动,不同的是,Bind方式需要传入三个参数,如下: Int ...

  6. Web API 2 入门——使用ASP.NET Web API和Angular.js构建单页应用程序(SPA)(谷歌翻译)

    在这篇文章中 概观 演习 概要 由网络营 下载网络营训练包 在传统的Web应用程序中,客户机(浏览器)通过请求页面启动与服务器的通信.然后,服务器处理请求,并将页面的HTML发送给客户端.在与页面的后 ...

  7. ezmorph-1.0.6.jar的作用

    使用json时要依赖的jar文件,在使用jquery的json时,没有引用该文件的时候容易报错.

  8. python 浮点运算 及 小数点精确位数

    >>> 1050 / 3133>>> 1050 / float(31)33.87096774193548                     # 分子或者分母用 ...

  9. GCD学习(五) dispatch_barrier_async

    先看段代码 dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISP ...

  10. 根据用户id生成一个唯一邀请码

    需求描述:根据用户id生成与之对应的唯一邀请码,范围为‘0-9A-Z’. 这个需求的重点在于加粗的部分,也就是要能够根据邀请码反推出用户ID,这样邀请码就不用入库了,在用户量很大的情况下,性能可以得到 ...