Search code examples
bashshellexpr

Bash index of first character not given


So basically something like expr index '0123 some string' '012345789' but reversed.
I want to find the index of the first character that is not one of the given characters...
I'd rather not use RegEx, if it is possible...


Solution

  • You can remove chars with tr and pick the first from what is left

    left=$(tr -d "012345789" <<< "0123_some string"); echo ${left:0:1}
    _
    

    once you have the char to find the index follow the same

    expr index "0123_some string" ${left:0:1}
    5