Shell中的${}、##和%%使用范例
假设定义了一个变量为,代码如下:
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中的${}、##和%%使用范例的更多相关文章
- 介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。
介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...
- shell 中的特殊符号的含义
来源:http://blog.sina.com.cn/s/blog_62a151be0100x9rn.html 第四章 基本功 - 特殊符号 学习撰写 script 最迅速的捷径是观摩别人的 scri ...
- shell 中的for、while循环及if语句
shell与其他语言一样也支持for.while循环 for循环的一般格式如下: #!/bin/sh for 变量 in 列表 do command command command ......... ...
- shell中脚本调试----学习
1.使用dos2unix命令处理在windows下开发的脚本 将windows下编辑的脚本放置到linux下执行的情况如下: [root@ks ~]# cat -v nginx.sh #!/bin/b ...
- shell中#*,##*,#*,##*,% *,%% *的含义及用法
介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...
- 转载:shell中#*,##*,#*,##*,% *,%% *的含义及用法
介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...
- linux shell 中的sleep命令
开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...
- shell 中的与、或表达式
今天总结一下linux shell中逻辑关机表达方式.逻辑与的表达: 1).if [ $xxx=a -a $xx=b ] 注:-a表示and的意思 2).if [ $xxx=a ] && ...
- shell简单用法笔记(shell中数值运算)二
shell中变量值,如果不手动指定类型,默认都是字符串类型: 例如: a= b= c=$a+#b echo $c 结果会输出:123+456 shell中,如果要进行数值运算,可以通过一下方法: 方法 ...
- shell中{}的妙用
shell中${}的妙用 1. 截断功能 ${file#*/}: 拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file##*/}: 拿 ...
随机推荐
- 怎么区分odd和even
odd [ɒd] 和even ['iːv(ə)n] 一个表示奇数.一个表示偶数 经常混淆. 一个记住的好方法: odd是3个字母,单数,所以表示奇数 even是4个字母,所以表示偶数
- 【Udacity】解决:字幕遮挡视频内容怎么办?Udacity字幕大小调整
字幕有没有办法显示在最下方,否则把内容挡住了,根本看不清老师所指的内容 Chrome 的小插件,能方便地调节视频的字幕大小,譬如:Udacity 的学习视频字幕大小不能调节,有时候不想它占太多位置,而 ...
- NgModelController: $setViewValue,$render,Formatter, Parser
NgModelController为ngModel directive提供了API.这个controller包含了关于data-binding,validation,css update, value ...
- Elasticsearch 架构原理
为什么要学习架构? Elasticsearch的一些架构设计,对我们做性能调优.故障处理,具有非常重要的影响.下面将从Elasticsearch的准实时索引的实现.自动发现.rounting和repl ...
- Mysql学习---SQL测试题之表结构
创建表结果和数据准备[直接执行即可] /* Navicat MySQL Data Transfer Source Server : ftl1012 Source Server Version : 50 ...
- 一句DOS命令搞定文件合并
用Dos的copy命令实现: copy a.js+b.js+c.js abc.js /b 将 a.js b.js c.js 合并为一个 abc.js,最后的 /b 表示文件为二进位文件,copy 命令 ...
- Doing Research Needs Efforts
What is research? From YouTube Video or baiduyun Links What does not? spend many hours before you ...
- July 11th 2017 Week 28th Tuesday
No possession, but use, in the only riches. 真正的财富不是占有,而是使用. These days I have bought tens of books a ...
- UI层实现
领域驱动设计实践 —— UI层实现 目录 1. User Interface 2. Controller 3. DTO 4. infrastructure层的公共组件 5. UI层类图 6. ...
- BZOJ3033:太鼓达人(DFS,欧拉图)
Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk.Poet_shy和ly ...