0

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 ?

2
  • Maybe use non-greedy version of *: /^.*?_some/? Commented Nov 25, 2013 at 7:24
  • Nope, that doesn't work either. See my answer below. Commented Nov 25, 2013 at 7:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You could have made this 30 character long by explaining why OPs version doesn't work.
"Unlike the regexes in most languages...": not true. Parameter expansion does not use regular expressions; it uses pattern matching. Where bash does use regular expressions (after the =~ operator), . has the standard meaning of "any single character".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.