A system administrator is writing a Bash script that requires validation of a user-provided version number string. The string should be in the format 'major.minor.patch' (e.g., '2.10.3'). The input is stored in a variable called VERSION. Which of the following conditional expressions should the administrator use to correctly validate the format of the VERSION variable?
if [ \(VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+\) ]
if [[ \(VERSION == "^[0-9]+\.[0-9]+\.[0-9]+\)" ]]
if (test \(VERSION -eq "^[0-9]+\.[0-9]+\.[0-9]+\)")
The correct answer uses the [[...]] conditional construct with the =~ operator. In Bash, the =~ operator, when used within double square brackets [[...]], performs an extended regular expression match. The regular expression ^[0-9]+\.[0-9]+\.[0-9]+$ correctly defines the 'major.minor.patch' format by anchoring the match to the start (^) and end ($) of the string and looking for one or more digits ([0-9]+) separated by literal periods (\.).
The expression [ $VERSION =~ ... ] is incorrect because the single bracket [ construct (the test command) does not support the =~ regex matching operator.
The expression [[ $VERSION == ... ]] is incorrect because the == operator inside double brackets performs glob-style pattern matching, not regular expression matching.
The expression (test $VERSION -eq ...) is incorrect because test is the same as the single bracket [ construct and does not support =~. Additionally, -eq is a numerical comparison operator, not a string or regex operator.
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.
Why does the `[[ ... ]]` construct support regex matching while `[ ... ]` does not?
Open an interactive chat with Bash
What does the regex `^[0-9]+\.[0-9]+\.[0-9]+$` mean in this context?
Open an interactive chat with Bash
What is the difference between `=` and `==` inside `[[ ... ]]`?
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