Shell脚本的学习笔记二:字符串
Shell 字符串
| 项目 | 功能 |
|---|---|
| 单引号 | 原样输出,变量无效。但可用成对单引号嵌套成对单引号输出变量 |
| 双引号 | 定义字符串中附带有变量的命令并且想将其解析后再输出的变量。 |
1. 单引号
#!/bin/bash
test='Try to do it.'
echo 'I say ${test}' #**[代码1]**
#echo ' \' ' #使用转义字符会报错,**[代码2]**
echo 'I say '${test}'' #**[代码3]**
单引号字符串的限制:
- 单引号里的任何字符都会原样输出,单引号字符串中的变量无效;[代码1]
- 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。[代码2]
- 可用成对单引号嵌套成对单引号输出变量。[代码3]
结果如下:
I say ${test} #**[代码1]**
I say Try to do it. #**[代码3]**
2. 双引号
#!/bin/bash
test="Try to do it."
echo "I say ${test}" #**[代码1]**
#echo " " " #不使用转义字符会报错,**[代码2]**
echo "I say '${test}' \"${test}\" " #使用转义字符,**[代码3]**
双引号字符串的优点:
- 双引号里可以有变量,将其解析后再输出。[代码1]
- 双引号里可以出现转义字符。[代码2][代码3]
I say Try to do it.
I say 'Try to do it.' "Try to do it."
3. 拼接字符串
#!/bin/bash
reader="Yogile"
#双引号
output1="This is "$reader" !"
output2="This is ${reader} !"
echo $output1 $output2
echo ""
#单引号
output3='This is '$reader' !'
output4='This is ${reader} !'
echo $output3 $output4
echo ""
#单双引号混合
echo $output1 $output3
输出结果为:
#双引号
This is Yogile ! This is Yogile !
#单引号
This is Yogile ! This is ${reader} !
#单双引号混合
This is Yogile ! This is Yogile !
4. 获取字符串长度
使用${#xxxx}(xxxx为变量名)输出字符串长度。
#!/bin/bash
reader="Yogile"
echo ${#reader} #输出 6
5. 提取、查找子字符串
同高级语言一样,Shell申请内存也从0位开始,0位指第 1 个字符。
${string:position:length}在从位置position开始提取长度为length的子串
string="This is Yogile. "
echo ""
#提取字符串
echo ${string:1:5} # 输出"his i"
echo ${string:12:4} # 输出"le. ",[代码xx]---------?:15位为空格,有输出
echo ""
#查找子字符串
#查找字符 i 或 g 的位置(计算最先出现的字母)
echo `expr index "$string" ig` # ` 是反引号,而不是单引号 '
6. 主要字符串操作(长度,读取,替换)
| 格式 | 含义 |
|---|---|
| ${#string} | $string的长度 |
| ${string:position} | 从位置$position开始提取子串 |
| ${string:position:length} | 在从位置$position开始提取长度为$length的子串 |
| ${string#substring} | 从变量${string}的开头, 删除最短匹配$substring的子串 |
| ${string##substring} | 从变量${string}的开头, 删除最长匹配$substring的子串 |
| ${string%substring} | 从变量${string}的结尾, 删除最短匹配$substring的子串 |
| ${string%%substring} | 从变量${string}的结尾, 删除最长匹配$substring的子串 |
| ${string/substring/replacement} | 使用$replacement, 来代替第一个匹配的$substring |
| ${string//substring/replacement} | 使用$replacement, 代替所有匹配的$substring |
| ${string/#substring/replacement} | 如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring |
| ${string/%substring/replacement} | 如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring |
Shell脚本的学习笔记二:字符串的更多相关文章
- Shell脚本的学习(二)
Shell脚本的学习(二) 方法: 1) 一个计算器: 2)递归实现打印目录 3)方法调用
- Mac下Shell脚本使用学习笔记(二)
参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 (7)Shell echo命令: 命令格式:echo string ①显示普通字符串:echo "It is a ...
- Shell脚本的学习笔记一:变量
三种变量: 局部变量:局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量. 环境变量:所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需 ...
- linux shell攻略学习笔记二
1.Cat命令 这么多命令,常用的 Cat –n file 显示文件以及行数 Cat - echo 'Text through stdin' | cat - file.txt Text throug ...
- Mac下Shell脚本使用学习笔记(一)
参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 1.使用终端创建test.sh: (1)进入指定文件夹路径(命令示例:cd Desktop/面向对象程序设计): (2)创 ...
- Linux Shell脚本编程学习笔记和实战
http://www.1987.name/141.html shell基础 终端打印.算术运算.经常使用变量 Linux下搜索指定文件夹下特定字符串并高亮显示匹配关键词 从键盘或文件里获取标准输入 [ ...
- shell脚本编程学习笔记(二)linux服务器启动流程
一.linux服务器启动流程 1.bios找到磁盘上的mbr主引导扇区 2.进入grub洁面选择相应的启动内核 3.读取kernel内核文件-/boot/vmlinuz-* 4.读取init的镜像文件 ...
- shell脚本编程学习笔记(一)
一.脚本格式 vim shell.sh #!/bin/bash //声明脚本解释器,这个‘#’号不是注释,其余是注释 #Program: //程序内容说明 #History: //时间和作者 二.sh ...
- Shell脚本编程基础笔记二
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8177697.html 一:输入 1:运行时参数 可以在启动脚本时,在其后输入参数. ./脚本 参数1 参数2. ...
随机推荐
- Eclipse Decompiler不生效解决办法
如下图,解决方案,Preferences->General->Editors->File Associations->*.class->Decompiler->De ...
- Oarcle的开始
1.数据库大致分类两种 1.关系型数据库(SQL) Oracle.Mysql(80%).DB2.Microsoft SQL Server.ProsgreSQL.Access.SQLSite 2.非关系 ...
- react脚手架搭建及配置
npm install -g create-react-app 装完之后,生成一个新的项目,可以使用下面的命令: create-react-app my-app cd my-app/yarn star ...
- 基于ROS和python,通过TCP通信协议,完成键盘无线控制移动机器人运动
一.所需工具包 1.ROS键盘包:teleop_twist_keyboard 2.TCP通讯包:socket $ cd ~/catkin_ws/src $ git clone https://gith ...
- DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined
DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined 原因:table 中定义的列和aoColumns ...
- java String 类型总结
java中String是个对象,是引用类型?,基础类型与引用类型的区别是,基础类型只表示简单的字符或数字,引用类型可以是任何复杂的数据结构,基本类型仅表示简单的数据类型,引用类型可以表示复杂的数据类型 ...
- Python Kivy writes / read the file on the SD card
Path to SD card from jnius import autoclass # SDcard Android # Get path to SD card Android try: Envi ...
- docker安装openwrt镜像(不完美案例)
镜像从http://downloads.openwrt.org/releases下载 注意选择generic-rootfs.tar.gz这种类型的镜像 使用docker import导入镜像,导入后可 ...
- Python云图——WordCloud了解一下
字符可以作画(参考前文:使用记事本画出照片) 字符串一样也可以 安装词云WordCloud. pip install wordcloud 编写要生成词云的内容字符串 保存为txt格式就可以了 使用Py ...
- 剑指offer(52)正则表达式的匹配
题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式 ...