美文网首页
shell变量替换总结

shell变量替换总结

作者: AntKing | 来源:发表于2020-02-18 11:07 被阅读0次

    1: ${变量#匹配规则} 从头开始匹配,最短删除

    variable_1="i love you, do you love me"
    var=${variable_1#*ov}
    echo $var
    

    输出结果 e you, do you love me

    2: ${变量##匹配规则} 从头开始匹配,最长删除

    variable_1="i love you, do you love me"
    var=${variable_1##*ov}
    echo $var
    

    输出结果e me

    3: ${变量%匹配规则} 从尾开始匹配,最短删除

    variable_1="i love you,do you love me"
    var=${variable_1%ov*}
    echo $var
    

    输出结果 i love you,do you l

    4: ${变量%%匹配规则} 从尾开始匹配,最长删除

    variable_1="i love you,do you love me"
    var=${variable_1%%ov*}
    echo $var
    

    输出结果 i l

    5: ${变量/旧字符串/新字符串} 用新字符串替换变零中第一个旧字符串

    var5="/usr/local/Cellar/dart/2.5.2/bin:/Users/lihe/Downloads/flutter/bin"
    var6=${var5/bin/BIN}
    echo ${var6}
    

    输出结果
    /usr/local/Cellar/dart/2.5.2/BIN:/Users/lihe/Downloads/flutter/bin

    5: ${变量//旧字符串/新字符串} 用新字符串替换变量中所有旧字符串

    var5="/usr/local/Cellar/dart/2.5.2/bin:/Users/lihe/Downloads/flutter/bin"
    var6=${var5//bin/BIN}
    echo ${var6}
    
    

    输出结果
    /usr/local/Cellar/dart/2.5.2/BIN:/Users/lihe/Downloads/flutter/BIN

    变量测试

    image.png

    相关文章

      网友评论

          本文标题:shell变量替换总结

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