You are writing a Bash script that should exit successfully when the value in the variable $alpha comes before or is identical to the value in $beta in lexicographic order. Which conditional test can be used inside an if statement to achieve this goal without causing a syntax error or forcing a numeric comparison?
(Assume both variables contain arbitrary ASCII strings and the script runs under /bin/bash.)
Inside Bash's [[...]] compound command, the operators for lexical string comparison are < (less than) and > (greater than). There are no built-in <= or >= operators for strings, so an expression such as [[ $alpha <= $beta ]] triggers a syntax error. Likewise, -le and its relatives are numeric operators; using them on strings either fails or produces misleading results. To emulate "less than or equal" for strings, you must combine the equality and less-than tests in a single expression using a logical OR. The correct option therefore uses [[ "$alpha" == "$beta" || "$alpha" < "$beta" ]], which returns true when the two strings are identical or when $alpha sorts before $beta. All other options either invoke an unsupported operator (<=), attempt a numeric comparison of non-numeric data (-le), or rely on [...] where < is parsed as a redirection symbol rather than a comparison 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.
What is lexicographic order in Bash?
Open an interactive chat with Bash
Why does Bash not support '<=' for string comparison?
Open an interactive chat with Bash
What is the difference between '[...]' and '[[...]]' in Bash?
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...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99
$19.99/mo
Billed monthly, Cancel any time.
3 Month Pass
$44.99
$14.99/mo
One time purchase of $44.99, Does not auto-renew.
MOST POPULAR
Annual Pass
$119.99
$9.99/mo
One time purchase of $119.99, Does not auto-renew.
BEST DEAL
Lifetime Pass
$189.99
One time purchase, Good for life.
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .