Search code examples
bashshellpattern-matchingparameter-expansion

Bash pattern matching 'or' using shell parameter expansion


hashrate=${line//*:/}
hashrate=${hashrate//H\/s/}

I'm trying to unify this regex replace into a single command, something like:

hashrate=${line//*:\+/H\/s/}

However, this last option doesn't work. I also tried with \|, but it doesn't seem to work and I haven't found anything useful in bash manuals and documentation. I need to use ${} instead of sed, even if using it solves my problem.


Solution

  • If you enable extended globbing (extglob via shopt), you can use the *(pattern1|pattern2|...) operator to match zero or more glob patterns:

    hashrate="${line//*(*:|H\/s)/}"