linux shell编程学习--字符串的使用和操作
来源:互联网 发布:软件工程推荐书籍知乎 编辑:程序博客网 时间:2024/05/19 16:19
在bash shell的使用过程中,经常会遇到一些字符串string的操作,下面是个人的一些使用总结。
一、字符串的定义
用双引号,单引号,或者直接在接在=后,都可以定义一个字符串,如下,定义了三个字符串
str1="this is a string" str2='this is a string'str3=this_is
但双引号和单引号,在bash中还是有区别的。
单引号引用,则保留字符串的所有字符的字面含义,同时,单引号内不允许再出现单引号,也不会对转义字符等进行转义。
双引号引用,则支持一些扩展,如$(美元符) \ (反斜杠)。\$可以引用后面的变量值,\(反斜杠)表示转义。如下:
str="test_sting."echo "\\$str" #输出结果为:\test_stringecho '\\$str' #输出结果为:\\$str
二、求字符串长度
str="test_string"echo ${#str} #得到长度9
三、字符串比较
bash shell中的字符串可以进行,大于,小于,等于,判断是否为空,是否有值等操作。在相等的操作中,支持正则匹配。具体语法如下:
[[ "1" < "2" ]] #true ASCII 字母顺序
> 大于 [[ "2" > "1" ]] #true ASCII 字母顺序
== 等于(支持模式匹配) [[ "aa" == "aa" ]] #true
[[ "aaa" == a* ]] #true 模式匹配
模式匹配的时候,不能加双引号 = 等于,与==等价 =~ 判断字串 [[ "aa" =~ "a" ]] #true
!= 不等于 [[ "a" != "b" ]] #true
-z 是否为空 [[ -z ""]] #true
-n 是否非空 [[ -n "a" ]] #true
bash shell中在比较字符串的时候,一般最好用”(双引号)将值包含起来,避免空格的影响。但在使用d**==(模式匹配)**的时候,则不能使用双引号
四、字符串的截断
可以使用#,##,%,%%等符号进行字符串的截断操作。bash shell的字符串支持四种截断方式,可以截断左边字符或者截断右边字符。
假设有字符串str
str="This-is-a-test-string"
echo ${str#*-} #得到is-a-test-string
## ##号截取,从左边截取掉最长的匹配,保留右边字符 echo ${str##*-} #得到string
% %截取,从右边截取最短的匹配,保留左边字符 echo ${str%-*} #得到This-is-a-test
%% %%截取,从右边截取最长的匹配,保留左边字符 echo ${str%%-*} #得到This
五、字符串的截取
shell字符串支持任意位置的子串截取。语法为:
${org_string:pos:length} 截取从pos开始的length长度的子串
${org_string:pos:-length} 截取从pos开始,截取到距字符串结束的length长度的位置的子串
${org_string:(-pos):length} 从距离字符串结束的pos位置开始,截取length长度的子串
${org_string:(-pos):-length} 从距离字符串结束的pos位置开始,截取到距字符串结束的length长度的位置的子串
${org_string:pos}截取从pos开始到字符串结束
${org_string:(-pos)}从距离字符串结束的pos位置开始,截取到字符串结束
具体用法,如下:
org_str="This-is-a-test-test-string"echo ${org_str:0:4} #得到 Thisecho ${org_str:0:-4} #得到 This-is-a-test-test-stecho ${org_str:(-11):4} #得到 testecho ${org_str:(-11):-4} #得到 testecho ${org_str:4} #得到 -is-a-test-test-stringecho ${org_str(-6)} #得到 string
六、字符串的替换
在bash shell的字符串中,可以使用新的字符串替换旧字符串中的子串。
假设有字符串org_str
org_str="This-is-a-test-test-string"
具体用法,如下表:
${org_str/test/TEST} #得到This-is-a-TEST-test-string
${org_str//sub_str/replace_str} 使用$replace_str的值来替换全部匹配的$sub_str ${org_str//test/TEST} #得到This-is-a-TEST-TEST-string
${org_str/#sub_str/replace_str} 如果$org_str是以$sub_str开始,使用$replace_str的值来替换$sub_str ${org_str/#This/THIS} #得到THIS-is-a-test-test-string
${org_str/%sub_str/replace_str} 如果$org_str是以$sub_str结束,使用$replace_str的值来替换$sub_str ${org_str/%string/STRING} #得到This-is-a-test-test-STRING
- linux shell编程学习--字符串的使用和操作
- [Linux]:shell 字符串操作
- linux shell 字符串操作
- linux shell 字符串操作
- linux shell字符串操作
- Linux Shell字符串操作
- linux--shell--字符串操作
- Linux Shell编程(15)——操作字符串
- linux查看当前使用的shell,修改shell,shell编程?
- shell 的字符串操作
- linux shell 字符串操作详解
- linux shell 字符串操作…
- Linux Shell编程学习
- Linux Shell编程学习
- Linux Shell编程学习
- Linux Shell编程学习
- linux shell编程学习
- 【学习】linux shell 编程
- 2013山东省第三届ACM省赛 The Best Seat in ACM Contest
- 理解GBDT算法(三)——基于梯度的版本
- 自定义FragmentTabHost中点击事件
- 检测算法对比_测试
- Minecraft源码分析(2) - Block,Section和Chunk
- linux shell编程学习--字符串的使用和操作
- Notepad++插件小结
- 命令模式
- JavaScript选项卡/页签/Tab的实现
- 递归与递推 - 青蛙过河
- BestCoder Round #82 (div.2) A B
- Public and Private Interfaces in ruby
- Windows无法访问VMWare虚拟机下CentOS的apache服务器
- 微软研发类实习生面试