I'm not aware of any existing equivalent. Some external tools may provide something out of the box, but not Bash. Bash is able to do this, but you need to tell it how to do this; i.e. you need to write some shell code.
The following shell function is an example of such code:
inStr () {
local start string substring len pos
start="$1"
string="$2"
substring="$3"
len="${#string}"
# strip start-1 characters
string="${string:start-1}"
# strip the first substring and everything beyond
string="${string%%"$substring"*}"
# the position is calculated
pos=$((start+${#string}))
# if it's more than the original length, it means "not found"
if [ "$pos" -gt "$len" ]; then
# not found, adjust the behavior to your needs
# e.g. you can print -1
return 1
else
printf "%s\n" "$pos"
return 0
fi
}
Usage:
inStr 6 "abcABC123ABCabc" "AB"
Notes:
In the line
string="${string%%"$substring"*}"
$substring
is double-quoted. Whatever you pass as substring will always be treated literally. E.g. A*a
will only match literal A*a
. If the line was like this:
string="${string%%$substring*}"
then you could pass a pattern to match. In this case A*a
could match the ABCa
fragment. Note you need to quote such substring while invoking the function, otherwise A*a
may get expanded to (possibly multiple) names of matching files (this term includes directories) in the current directory (globbing).
- The function does not validate its input; in particular it produces garbage when a nonpositive starting position is used. Implement some tests if needed.
- The behavior for edge cases (empty string and/or substring) may not be as you expect. Implement some logic if needed.