• shell 的 ${ }中 ##、%%、// 使用方法及举例


    使用说明

    #与##

    ${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var.
    ${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.

    %与%%

    ${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var.
    ${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end of $var.

    简言之:
    #是将${var}中的左边(键盘上 #$ 的左边)的${Pattern}去掉;
    %是将${var}中的右边(键盘上 %$ 的右边)的${Pattern}去掉;
    其中单一符号#%是最小匹配;两个符号##%%则是最大匹配。

    /与//

    ${string/substring/replacement} Replace first match of $substring with $replacement.
    ${string//substring/replacement} Replace all matches of $substring with $replacement.

    示例1

    #!/bin/sh
    
    str="This is This is my test string search_string"   #被测试字符串
    subStr1="This is "                                   #待删除的字符串1
    subStr2="string"                                     #待删除的字符串2
    
    echo -e "Test 1 ${str#$subStr1}"
    echo -e "Test 2 ${str##$subStr1}"
    echo -e "Test 3 ${str#$subStr2}"
    echo -e "Test 4 ${str##$subStr2}"
    echo -e "Test 5 ${str#*$subStr1}"
    echo -e "Test 6 ${str##*$subStr1}"
    echo -e "Test 7 ${str##*$subStr1*}"
    echo -e "Test 8 ${str##*$subStr2}"
    echo -e "Test 9 ${str##*$subStr2*}"
    echo -e
    echo -e "Test 10 ${str%$subStr1}"
    echo -e "Test 11 ${str%%$subStr1}"
    echo -e "Test 12 ${str%$subStr2}"
    echo -e "Test 13 ${str%%$subStr2}"
    echo -e "Test 14 ${str%*$subStr1}"
    echo -e "Test 15 ${str%%$subStr1*}"
    echo -e "Test 16 ${str%*$subStr1*}"
    echo -e "Test 17 ${str%$subStr2*}"
    echo -e "Test 18 ${str%*$subStr2}"
    echo -e "Test 19 ${str%%*$subStr2}"
    echo -e "Test 20 ${str%%$subStr2*}"
    echo -e "Test 21 ${str%%*$subStr2*}"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    其输出为:

    Test 1 This is my test string search_string
    Test 2 This is my test string search_string
    Test 3 This is This is my test string search_string
    Test 4 This is This is my test string search_string
    Test 5 This is my test string search_string
    Test 6 my test string search_string
    Test 7
    Test 8
    Test 9
    
    Test 10 This is This is my test string search_string
    Test 11 This is This is my test string search_string
    Test 12 This is This is my test string search_
    Test 13 This is This is my test string search_
    Test 14 This is This is my test string search_string
    Test 15
    Test 16 This is
    Test 17 This is This is my test string search_
    Test 18 This is This is my test string search_
    Test 19
    Test 20 This is This is my test
    Test 21
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    示例2

    #!/bin/sh
    
    str="This is This is my test string search_string"  #被测试字符串
    subStr="This is "                                   #待替换的字符串
    replaceStr="TTT "                                   #替换的字符串
    
    echo -e "Test 1 ${str/$subStr/$replaceStr}"
    echo -e "Test 2 ${str//$subStr/$replaceStr}"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    其输出为:

    Test 1 TTT This is my test string search_string
    Test 2 TTT TTT my test string search_string
    
    • 1
    • 2

    Reference

    HTML 版本:Advanced Bash-Scripting Guide -10.2. Parameter Substitution
    PDF 版本:Advanced Bash-Scripting Guide P133页

  • 相关阅读:
    图像的离散傅里叶变换-python实战
    Golang | Leetcode Golang题解之第49题字母异位词分组
    HTTP基础验证
    【深入理解Kotlin协程】lifecycleScope源码追踪扒皮
    uniapp canvas 无法获取 webgl context 的问题解决
    对比CentOS与Ubuntu:选择最适合你的Linux发行版
    谈谈什么是数据质量管理
    产品经理基础--01认识产品经理
    谷歌浏览器jsonView插件安装与使用
    Python实现批量采集美女shipin<无水印>
  • 原文地址:https://blog.csdn.net/lm_hao/article/details/126894676