Skip to main content
1 of 8
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

For completeness, while case has a | OR operator, it doesn't have an AND operator but if using shells with extended glob operators (ksh, zsh, bash), you can implement the AND in the pattern syntax:

  • ksh93's @(x&y&z) operator:

      case $string in
        ( @({12}(?)&~(i:[aeiou]*)&*[0123456789]) )
          echo is 12 characters long AND starts with a vowel and ends in a decimal
      esac
    
  • zsh (using ~ (AND-NOT) combined with ^ (NOT)): x~^y&~z

      set -o extendedglob
      case $string in
        ( ?(#c12)~^(#i)[aeiou]*~^*[0-9] )
          echo is 12 characters long AND starts with a vowel and ends in a decimal
      esac
    
  • ksh88, bash, using double negation with OR (!(!(x)|!(y)|!(z)))

      shopt -s extglob # bash only
      case $string in
        ( !(!(????????????)|!([aAeEiIoOuU]*)|!(*[0123456789]) )
          echo is 12 characters long AND starts with a vowel and ends in a decimal
      esac
    

In any case, remember that except in zsh where ranges are always based on codepoint values, ranges like [0-9] cannot be used reliably outside of the POSIX/C locale (hence the [0123456789] instead above).

Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k