ACE-P-ALE1-04 practice question 28 of 35
In bash scripting, which of the following comparison operators are used on strings?
Full set: 100 questionsIn bash scripting, the == operator is used for string equality comparison. It is most commonly used within the double-bracket [[ ... ]] conditional construct, which is an extension to the POSIX [ ... ] test command. While the single = is the POSIX-compliant operator for string equality, bash treats == as a synonym for = within [[ ... ]], making it a valid and often preferred choice for string comparisons due to its similarity with other programming languages. The other operators listed are used for integer arithmetic comparisons or file type tests, not for comparing string values.
A. -gt: This is an arithmetic comparison operator that tests if one integer is "greater than" another.
B. -eq: This is an arithmetic comparison operator that tests if two integers are "equal".
D. <=: This is not a valid comparison operator in bash conditional expressions. The correct operator for "less than or equal to" for integers is -le.
E. -f: This is a file test operator that returns true if the given path exists and is a regular file.
1. GNU Bash Reference Manual, Section 6.4: Bash Conditional Expressions. The manual states, "When used with [[, the operators sort lexicographically using the current locale. The == and != operators are also available." It also lists -eq, -gt, and other similar operators under "Arithmetic Binary Operators".
2. MIT, "The Missing Semester of Your CS Education", Lecture 2: Shell Tools and Scripting, "Conditionals" section. The course material explicitly differentiates between string and integer comparisons, noting: "Note that bash is a bit tricky in its handling of strings and integers... use == for strings and -eq for integers."
Keep practicing
Take the free ACE-P-ALE1-04 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other ACE-P-ALE1-04 practice questions
- Question 1When using the sudo command to run something as root, which password is required?
- Question 2Which utility can be used to remove sections or columns of each line of input?
- Question 3The ps –ef command is from which UNIX variant?
- Question 4In file globbing, which symbol matches any single character?
- Question 5If the system is swapping, what can be done to alleviate the problem?
- Question 6When scripting in bash, it is recommended to always use which symbols around your variabl…
