Inside the [[ ]] construct the =~ operator interprets the right-hand word as a POSIX extended regular expression. Quoting that word forces Bash to treat the characters literally, so the comparison searches for the text ^[A-Z]{3}-[0-9]{4}$ inside CPU-1234 and fails. Removing the quotes exposes the regex, the pattern matches any part of the string, and the function prints valid. Replacing =~ with ==, toggling extglob, or reversing the operands do not address the quoting issue and still leave the test failing.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the `=~` operator in Bash do?
Open an interactive chat with Bash
Why does removing quotes around `$re` fix the issue?
Open an interactive chat with Bash
How does POSIX extended regular expressions work in Bash's `[[ ]]` test?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access