字符串
字符串可以用单引号,也可以用双引号,也可以不用引号。
单引号
:
- 单引号里的任何字符都会
原样输出
,包含变量。 - 单引号字串中不能出现单独单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。
#!/bin/bash
name='LiLei'
echo 'My name is '$name
echo 'My name is $name'
echo 'My name is ''LiLei'
#sh test.sh
My name is LiLei
My name is $name
My name is LiLei
双引号
:
- 双引号里可以有
变量
- 双引号里可以出现
转义字符
- 双引号可作为字符串
拼接
使用
#!/bin/bash
name='LiLei'
echo "My name is "$name
echo "My name is $name"
echo "My name is '$name'"
echo "My name is \"$name\""
echo "My name is ""LiLei"
#sh test.sh
My name is LiLei
My name is LiLei
My name is 'LiLei'
My name is "LiLei"
My name is LiLei
计算字符串长度
记得加大括号{}
#!/bin/bash
name='LiLei'
echo ${#name}
#sh test.sh
5
字符串切片
#!/bin/bash
name='LiLei'
echo ${name:2:4}
#sh test.sh
Lei
网友评论