Shell脚本之:字符串

字符串可以用单引号,也可以用双引号,也可以不用引号。

单引号

str='this is a string'

单引号字符串的限制:

1.单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;

2.单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

双引号

name='runnyu'
str="Hello, ${name} 
"

双引号的优点:

1.双引号里可以有变量

2.双引号里可以出现转义字符

获取字符串长度

string="abcd"
echo ${#string} #输出 4

提取字字符串

string="hello world"
echo ${string:1:4} #  ${string:index:length}

查找子字符串

string="hello world"
echo `expr index "$string" world`   #   `expr index "$string" substring`
原文地址:https://www.cnblogs.com/runnyu/p/4676627.html