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

  • 相关阅读:
    IDEA的Facets添加web后没有反应
    中集集团人工智能企业CIMCAI中集飞瞳,深入贯彻国家关于智慧港口数字港口建设部署要求全球顶尖港航AI核心技术打造超一流智慧港口
    【JAVA基础之IO】字节流、字符流以及乱码问题
    ABBYY FineReader PDF15免费版图片文件识别软件
    JAVA高级技术入门(单元测试,反射,注解,动态代理)
    基于Echarts实现可视化数据大屏产业大数据指挥舱系统
    playwright自动化项目搭建
    java-php-python-公益诊疗系统计算机毕业设计
    校园安保巡逻机器人
    数据结构-快速排序“人红是非多”?看我见招拆招
  • 原文地址:https://blog.csdn.net/lm_hao/article/details/126894676