简体   繁体   中英

Replace "/" in a string using shell script

PATH="/dir1/subdir1"

Want to replace "/" with "-" from the above string and the result should be

-dir1-subdir1

You can use tr for that since you want to replace a single character with another one:

echo $PATH | tr "/" "-"

Result is: -dir1-subdir1

In bash you can use a pattern substitution of the form ${parameter//pattern/string} to replace all matches of pattern with string :

echo "${PATH//\//-}"

Note that your pattern string / must be escaped with \\/ .

You can check the more detailed version.

$ var="001244abc"
$ replace="polo"
$ echo ${var//001244/$replace}
poloabc

Hope it helps..

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