A system administrator is writing a shell script that must check if a given username, stored in the USERNAME variable, comes alphabetically after the letter "m". Which of the following conditional statements correctly implements this string comparison?
The correct answer uses the extended test construct [[...]] with the > operator. In Bash, when the > operator is used within [[...]], it performs a lexicographical (alphabetical) comparison based on the current locale's collation order. It correctly determines if the string on the left ($USERNAME) is "greater than" (comes after) the string on the right ("m").
The option if [ "$USERNAME" > "m" ] is incorrect because inside the legacy [ test construct, the > character is treated by the shell as an output redirection operator, not a comparison operator. This command would attempt to write the output of [ "$USERNAME" ] into a file named m.
The option if [[ "$USERNAME" -gt "m" ]] is incorrect because -gt is a numerical comparison operator (greater than) and should be used for comparing integers, not for lexicographical string comparison.
The option if ( "$USERNAME" > "m" ) is incorrect as parentheses (...) are used to execute commands in a subshell, not to perform conditional tests of this type. The correct construct for conditional tests is if [[...]] or if [...].
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 is [[...]] preferred over [...] for string comparison in Bash?
Open an interactive chat with Bash
What happens if I use -gt for string comparison in Bash?
Open an interactive chat with Bash
How does the Bash shell treat the > operator in [ ... ]?
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