Novice C Novice C - 1 year ago 54
Bash Question

Shell string manipulation, wildcard scope

I am attempting to manipulate a string in shell by removing a substring, separated by some delimiters. Currently I am having some undesired behavior from the wildcard character as follows:

echo ${string/bad*.} #start.end

In the above example say I know the substring I seek to remove starts with
followed by random
all of which I wish to remove. I want to keep the string
, however, in this example the wildcard takes the widest scope instead of the narrowest (taking
instead of the desired behavoir

Is there a way to limit the scope of the wildcard to stop at the first
, without knowing what
might be?

Answer Source

With enabled ext. glob (see on this page for details)

shopt -s extglob

you could do

echo ${string/bad*([^.]).}

which results in