菜鸟教程提供的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脚本的学习笔记二:字符串的更多相关文章

  1. Shell脚本的学习(二)

    Shell脚本的学习(二) 方法: 1) 一个计算器: 2)递归实现打印目录    3)方法调用

  2. Mac下Shell脚本使用学习笔记(二)

    参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 (7)Shell echo命令: 命令格式:echo string ①显示普通字符串:echo "It is a ...

  3. Shell脚本的学习笔记一:变量

    三种变量: 局部变量:局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量. 环境变量:所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需 ...

  4. linux shell攻略学习笔记二

    1.Cat命令 这么多命令,常用的 Cat –n file  显示文件以及行数 Cat - echo 'Text through stdin' | cat - file.txt Text throug ...

  5. Mac下Shell脚本使用学习笔记(一)

    参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 1.使用终端创建test.sh: (1)进入指定文件夹路径(命令示例:cd Desktop/面向对象程序设计): (2)创 ...

  6. Linux Shell脚本编程学习笔记和实战

    http://www.1987.name/141.html shell基础 终端打印.算术运算.经常使用变量 Linux下搜索指定文件夹下特定字符串并高亮显示匹配关键词 从键盘或文件里获取标准输入 [ ...

  7. shell脚本编程学习笔记(二)linux服务器启动流程

    一.linux服务器启动流程 1.bios找到磁盘上的mbr主引导扇区 2.进入grub洁面选择相应的启动内核 3.读取kernel内核文件-/boot/vmlinuz-* 4.读取init的镜像文件 ...

  8. shell脚本编程学习笔记(一)

    一.脚本格式 vim shell.sh #!/bin/bash //声明脚本解释器,这个‘#’号不是注释,其余是注释 #Program: //程序内容说明 #History: //时间和作者 二.sh ...

  9. Shell脚本编程基础笔记二

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8177697.html 一:输入 1:运行时参数 可以在启动脚本时,在其后输入参数. ./脚本 参数1 参数2. ...

随机推荐

  1. ansible常用命令大全

    ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s   ...

  2. springmvc sessionfilter 登录过滤器

    1.在web.xml中配置 <!-- sessionfilter --> <filter> <filter-name>sessionFilter</filte ...

  3. Java 文件重命名

    Java 文件重命名 /** * 重命名文件 * @param fileName * @return */ public static void renameFile(String filePath, ...

  4. saltstack 基本的批量操作

    centos 6.5 saltstack 2015.5.10 (Lithium) 基本用法 # salt 'DEV-APP-001' cmd.run 'hostname' #指定被控端 # salt ...

  5. 分析hello1项目里面的web.xml

    在example目录下的web\jsf\hello1\target\hello1\WEB-INF路径里可以找到hello1的web.xml <?xml version="1.0&quo ...

  6. jsp/servlet学习四之jsp初窥

    jsp页面本质上是一个servlet,jsp页面是一个以.jsp结尾的文本文件. jsp自带的API包含4个包: javax.servlet.jsp.包含用于servlet/jsp容器将jsp页面翻译 ...

  7. 使用vue.js + laravel开发单页面应用

    最近学了vuejs和laravel,然后顺便就撸了简单的demo,这里将会根据这个demo介绍下如何使用vuejs+laravel开发一个简单的单页面应用,demo的github地址是https:// ...

  8. vue--实例化对象

    根目录下的文件,这些是创建vue项目时生成的配置文件 node_modules=> 里面的文件是项目开发过种中的各种依赖,我们暂且不用去深入了解: public=> 主要放的是一些公用的文 ...

  9. 1.6 安全认证与授权(springboot与安全)

    引言:以下文档是学习尚硅谷关于springboot教学视频后整理而来! 一.安全 认证(Authentication):证明你是谁? 授权(Authorization):你能干什么? 参考资料: Sp ...

  10. postman(六):详解在Pre-request Script中如何执行请求

    上一篇借着如何在不同接口之间传递数据,简单说了下在postman编写脚本发送请求,这里再详细介绍一下如何在Pre-request Script和Tests标签中编写脚本.因为我目前研究的也不是很深,对 ...