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 questions
Answer options
A: -gt
B: -eq
C: == Correct answer
D: <=
E: -f
Correct Answer:
C. ==
Explanation

In 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.

Why Incorrect

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.

References

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."

Scroll to Top