How do I replace the first string pattern in a variable using a bash shell script?



  • Usage is ${name/pattern/string}

    declare -A MyBucketList
    MyBucketList=([1]="The rain in Spain stays mainly on the plain" [2]="Go to Mars" [3]="Climb Mount Everest")
    echo ${MyBucketList[2]/Go/Just Been}
    

    Running the script gives the following results. String Go has been replaced with Just Been.

    $ ./associative_array.sh
    Just Been to Mars
    

    This usage applies to all string variables, not just arrays.


Log in to reply
 

© Lightnetics 2024