Search code examples
shellunixposix

Matching extended regular expressions in POSIX Shell


I think I have a really simple question but i can't figure out the answer. I have this code [[ "${file_name}" = ${regex} ]] && continue POSIX version does not support this [[ ]] pattern... Basically what this line does is. If name of the file matches the Extended regular expression, it skips my loop. Is there a way in POSIX norm to do this same thing on one line? or is there any other way to have a word and compare it with E regular expression? Thank you for your answers.


Solution

  • Use grep -E:

    if grep -qE "$regex" "$file_name"; then
    

    If you want to match the content of the variable file_name, pipe it to grep:

    if printf "%s\n" "$file_name" | grep -qE "$regex"; then