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

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.

最新文章

  1. C# 对 App.config的appSettings节点数据进行加密
  2. SQL-数学、字符串、时间日期函数和类型转换
  3. mysql 数据库基本概念
  4. MongoDB,客户端工具备份数据库
  5. Sequence.js - 适合电子商务网站的图片滑块
  6. Android设置窗口、控件透明度
  7. 阅读《Oracle内核技术揭秘》的读书笔记
  8. 最新发布C#.NET快速开发框架企业版V4.0 (适合开发ERP、进销存系统)
  9. js判断IP js判断域名
  10. AVOS_百度百科
  11. T-SQL基础(5) - 表表达式
  12. margin 相关 bug 系列
  13. poj2349,最小生成树!
  14. JVM内存结构和6大区域
  15. IOS UIScrollView常用代理方法
  16. 1、roboguide新建工程文件
  17. Codeforces Round #487 (Div. 2) C - A Mist of Florescence
  18. BZOJ百题版切计划(不咕)
  19. 探讨android更新UI的几种方法
  20. Excel 一个工作表进行按行数拆分

热门文章

  1. 基础架构之Mongo
  2. 项目经验:GIS<MapWinGIS>建模第七天
  3. 浅谈回归Regression(一)
  4. 通过JavaScript创建Qml对象
  5. centos 开机执行的命令
  6. 如何在 Azure 中的 Linux 经典虚拟机上设置终结点
  7. 解决maven工程无法创建src/main/java包名的方法
  8. Routing Manager for WCF4 z
  9. Linux下SVN安装配置及应用
  10. 如何控制table中td内的文本位置