美文网首页
Shell 字符串

Shell 字符串

作者: tafanfly | 来源:发表于2019-07-30 16:17 被阅读0次

    字符串

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

    单引号:

    • 单引号里的任何字符都会原样输出,包含变量。
    • 单引号字串中不能出现单独单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。
     #!/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
    

    相关文章

      网友评论

          本文标题:Shell 字符串

          本文链接:https://www.haomeiwen.com/subject/pvcekctx.html