简体   繁体   中英

Equivalent in bash string substitution of this sed substitution

What would be the equivalent bash substitution of this sed substitution?

STRING="HELLO-MY-NAME-IS"
$ echo $STRING | sed 's/-/%2d/g'
HELLO%2dMY%2dNAME%2dIS

I have tried with this, but only applies to the first substring found:

$ echo ${STRING/"-"/"%2d"}
HELLO%2dMY-NAME-IS

Thanks

You may give a try to:

$ echo ${STRING//-/%2d}
HELLO%2dMY%2dNAME%2dIS

Note that you were using a single slash after STRING , while it is needed two of them to have a global replacement.

               .
$ echo ${STRING/-/%2d}
HELLO%2dMY-NAME-IS
               ..    
$ echo ${STRING//-/%2d}
HELLO%2dMY%2dNAME%2dIS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM