Shell script substring from first indexof substring Shell script substring from first indexof substring unix unix

Shell script substring from first indexof substring


You can do:

$ a="some long string"$ b="ri"$ echo $a | grep -o "$b.*"ring


Try:

    $ a="some long string"    $ b="ri"    $ echo ${a/*$b/$b}    ring    $ echo ${a/$b*/$b}    some long stri


grep, sed and so on can be used but it is not pure-bash.

expr is a good choice but index parameter is not, because it matches character not the whole string, try with a = "some wrong string" it matches the first r.

Instead use expr match with its regular expression parameter :

a="some long string";b="ri";echo ${a:$(expr match "$a" ".*${b}") - $(expr length "$b")}

It also works with a = "some wrong string"