shell学习笔记之二——字符串



  • 字符串

    单引号

    单引号内不能引用变量

    单引号内不能出现单独一个单引号(转义后也不行),可以用于拼接字符串

    str='asd as d asd  asd'
    

    双引号

    双引号内可以有变量

    双引号内可以转义

    your_name='zyx'
    str="Hello, I know you are \"$your_name\"! \n"
    echo -e $str
    

    拼接字符串

    your_name="zyx"
    # 使用双引号拼接
    greeting="hello, "$your_name" !"
    greeting_1="hello, ${your_name} !"
    echo $greeting  $greeting_1
    # 使用单引号拼接
    greeting_2='hello, '$your_name' !'
    greeting_3='hello, ${your_name} !'
    echo $greeting_2  $greeting_3
    

    输出

    hello, runoob ! hello, runoob !
    hello, runoob ! hello, ${your_name} !
    

    获取字符串长度 提取、查找子字符串

    获取长度

    str="asdc"
    echo ${#str}
    

    提取子字符串

    str="asdasdasdasd"
    echo ${str:1:4}
    

    查找子字符串

    str="123asdijkao"
    echo `expr index "$str" io`
    #反引号`
    

 

Copyright © 2018 bbs.dian.org.cn All rights reserved.

Looks like your connection to Dian was lost, please wait while we try to reconnect.