简体   繁体   中英

Bash string regex replace with “.*”

for a string

s='abc_somedef'

use regex replace (it works)

echo ${s//_some/}

use regex replace with .* (it NOT works)

echo ${s//^.*_some/}

I want the result to be def

how I write it with bash internally (not sed/awk) ? maybe some escape char ?

Use this:

echo ${s//*_some/}

Your version didn't match since it was trying to match a dot literally.

UPDATE:

Chepner has correctly explained below that * here is not part of a regex at all. It is simply being used as a globbing character.

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