Regex in case statement

10,495

That is because case doesn't use regex's but bash Pathname Expansion. You can learn more from bash man page or from Bash Reference Manual.

Share:
10,495

Related videos on Youtube

Mud Bungie
Author by

Mud Bungie

Updated on September 18, 2022

Comments

  • Mud Bungie
    Mud Bungie almost 2 years

    I'm having a hard time getting regex matches to work in a bash case statement.

    Example code:

    #!/bin/bash                          
    
    str='    word1 word2'
    
    echo "With grep:"
    echo "$str" |grep '^\s*\<word1\>'
    
    echo "With case:"
    case "$str" in
        '^\s*\<word1\>') echo "$str" ;;
    esac
    

    The example works with grep, but not with case... I'm confused, because some simpler regexes work with case. Does case use a different syntax for regex? Am I just not escaping things properly?

    • heemayl
      heemayl about 8 years
      case does not support regex, it supports shell globbing.
  • Conner Dassen
    Conner Dassen over 4 years
    Great to link to the manual.