How do I remove the shortest & longest leading patterns of a substring in bash shell script?



  • Remove shortest leading substring that matches pattern.

    Usage is ${name#pat}

    Remove longest leading substring that matches pattern.

    Usage is ${name##pat}

    declare -A MyBucketList MyKtbList MyList
    MyBucketList=([1]="See The See Grand Canyon" [2]="Go to Mars" [3]="Climb Mount Everest")
    echo ${MyBucketList[1]#S*e }
    

    Line 3 has a space at the end in the pattern. The result here is to remove the shortest leading substring that matches pattern. I entered some extra words in the array for demonstration purposes.

    $ ./associative_array.sh
    The See Grand Canyon
    

    Now changing line 3 to echo ${MyBucketList[1]##S*e } to remove the longest leading substring that matches pattern.

    $ ./associative_array.sh
    Grand Canyon
    

Log in to reply
 

© Lightnetics 2024