Can you use regex as the identifier in a Bash case statement?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

Can you use regex as the identifier in a Bash case statement?

Explanation:
Bash case statements rely on shell pattern matching rather than full regular expressions. The patterns you use in a case are glob patterns: * matches any string, ? matches a single character, and [set] matches a character class. You can enable extended globbing for more forms, but these remain glob patterns, not regex. Because of that, you can’t write a case pattern that uses regular-expression syntax. If you need true regex matching, use the test form with the =~ operator, like [[ "$var" =~ regex ]], which applies a POSIX regular expression to the string. So the correct idea is that case uses globs, not regex.

Bash case statements rely on shell pattern matching rather than full regular expressions. The patterns you use in a case are glob patterns: * matches any string, ? matches a single character, and [set] matches a character class. You can enable extended globbing for more forms, but these remain glob patterns, not regex.

Because of that, you can’t write a case pattern that uses regular-expression syntax. If you need true regex matching, use the test form with the =~ operator, like [[ "$var" =~ regex ]], which applies a POSIX regular expression to the string. So the correct idea is that case uses globs, not regex.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy