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

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. RocketMQ读书笔记1——简述

    [消息队列的功能介绍] 分布式消息队列可以提供应用解耦.流量削峰.消息分发.保证最终一致性.方便动态扩容等功能. [MQ使用场景1——应用解耦] 复杂的系统如电商系统,会存在多个子系统,如订单系统.库 ...

  2. Android常用的图片加载库

     Android常用的图片加载库 前言:图片加载涉及到图片的缓存.图片的处理.图片的显示等.四种常用的图片加载框架,分别是Fresco.ImageLoader. Picasso. Glide. Uni ...

  3. win7 64 位安装 python,提示: 0x80240017-未指定的错误

    首先确保从python官网下载正确的python版本: https://www.python.org/downloads/windows/ 然后,应该是 VC 相关的问题. 去微软网站下载下列补丁包即 ...

  4. mac和windows自动清理内存工具

    因为我比较懒,所以需要一款能自动清理电脑内存的工具,目的是设置内存最小值,然后自动清理. mac: drcleaner windows: MaxMem win10设置开机启动地址:C:\Program ...

  5. 代码分析工具推荐Understand

    之前看ogitor ,第一次看到那么多代码~~不知道从哪里下手,而且好多东西都不会Ogre什么的都不是很清楚,对ogitor的代码结构的了解就更不用提了.晕头转向的 不知道从哪里下手,一点点的看起来好 ...

  6. 【Leetcode】【Medium】Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. Elasticsearch、MongoDB和Hadoop比较

    IT界在过去几年中出现了一个有趣的现象.很多新的技术出现并立即拥抱了“大数据”.稍微老一点的技术也会将大数据添进自己的特性,避免落大部队太远,我们看到了不同技术之间的边际的模糊化.假如你有诸如Elas ...

  8. Python切片(入门7)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407977.html 本文出自:[Edwin博客园] Python切片 1. 对list进行切片 L = r ...

  9. jemter 使用if控制器,选择需要的内容

    背景:需要根据人员传入的变量,来选择运行的环境,调用不同的参数,进行拼接,使用到if控制器 取到的数据,调用的就是test1的数据

  10. MySQL语法二:数据操纵语句

    数据操纵语句DML(SELECT,DELETE,UPDATE,INSERT) 一. 数据操纵语句是对数据表中的内容进行操作.比如对某个表中的某条记录或者多条记录进行增删改查操作. 一).查询 SELE ...