A system administrator is writing a bash script to process application log files. The script must verify that a filename, stored in the logfile variable, adheres to the format app-YYYY-MM-DD.log before processing it. The logfile variable currently holds the value app-2025-08-05.log.
Which of the following conditional statements will correctly validate the filename format?
if [[ "\(logfile" =~ ^app-[0-9]{4}-[0-9]{2}-[0-9]{2}\.log\) ]]
if [ "\(logfile" =~ ^app-[0-9]{4}-[0-9]{2}-[0-9]{2}\.log\) ]
if [[ "$logfile" == "app---*.log" ]]
if [[ "\(logfile" -eq "app-[0-9]{4}-[0-9]{2}-[0-9]{2}\.log\)" ]]
The correct answer uses the [[ ... ]] conditional construct with the =~ operator. In bash, [[ ... ]] is a keyword that provides extended features for tests. The =~ operator, when used within [[ ... ]], matches the string on the left against the extended regular expression on the right. The pattern ^app-[0-9]{4}-[0-9]{2}-[0-9]{2}\.log$ correctly matches a string that starts with app-, followed by four digits, a dash, two digits, a dash, two digits, a literal dot (\.), and ends with log.
The option using == "app-*-*-*.log" is incorrect because == inside [[...]] performs pattern matching using globbing rules, not regular expressions, and the asterisks here would not correctly validate the digit-only format of the date components.
The option using [ ... =~ ... ] is incorrect because the =~ operator is a feature specific to the [[ ... ]] construct in bash and is not defined for the POSIX [ command.
The option using -eq is incorrect because -eq is a numerical comparison operator used to check if two integers are equal and will result in an error when used with non-integer strings.
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 is the difference between `[[ ... ]]` and `[ ... ]` in bash?
Open an interactive chat with Bash
What is the purpose of the `=~` operator in bash?
Open an interactive chat with Bash
Why does `==` behave differently from `=~` in bash conditionals?
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