What's special about -eq compared to = and == ?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

What's special about -eq compared to = and == ?

Explanation:
In shell tests, there are dedicated operators for numeric versus string comparisons. The -eq operator is the numeric equality test: it treats both operands as integers and compares their numeric values. If either operand isn’t a valid integer, the test fails with an error like “integer expression expected,” which is what “strings may fail” is pointing to. In contrast, the = and the == operators are used for string comparisons. They compare the textual content of the operands rather than interpreting them as numbers. In Bash, = is the standard string equality operator in POSIX-style tests, while == inside the Bash-style [[ ... ]] test also handles string comparison and can support pattern matching with wildcards. So the special thing about -eq is that it enforces numeric comparison, whereas = and == are for comparing text.

In shell tests, there are dedicated operators for numeric versus string comparisons. The -eq operator is the numeric equality test: it treats both operands as integers and compares their numeric values. If either operand isn’t a valid integer, the test fails with an error like “integer expression expected,” which is what “strings may fail” is pointing to.

In contrast, the = and the == operators are used for string comparisons. They compare the textual content of the operands rather than interpreting them as numbers. In Bash, = is the standard string equality operator in POSIX-style tests, while == inside the Bash-style [[ ... ]] test also handles string comparison and can support pattern matching with wildcards.

So the special thing about -eq is that it enforces numeric comparison, whereas = and == are for comparing text.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy