• 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页

  • 相关阅读:
    C++ 堆、大顶堆、小顶堆、堆排序
    CentOS中实现基于Docker部署BI数据分析
    Linux 实用命令
    函数调用分析
    Shopee店铺ID是什么?Shopee店铺id怎么看?——站斧浏览器
    【RocketMQ】消息的消费总结
    记一次 HTTPS 抓包分析和 SNI 的思考
    java毕业设计坝上长尾鸡养殖管理系统Mybatis+系统+数据库+调试部署
    如何管理“问题成员”?手段要“狠”
    C#开发的OpenRA游戏之游戏设计思路
  • 原文地址:https://blog.csdn.net/lm_hao/article/details/126894676