The comparison operators compare two numbers and give true or false. They’re how a configuration checks a value against a limit, such as a port that must be at least 1024.
Syntax
Syntax
<A> < <B>
<A> <= <B>
<A> > <B>
<A> >= <B>
<A>,<B>: two numbers.<less than,<=less than or equal,>greater than,>=greater than or equal.
The docs say these operators expect numbers: Comparison Operators.
Prerequisites
- OpenTofu® installed. Installing OpenTofu in the OpenTofu docs covers every system.
Step 1: Try It in tofu console
Open a terminal in any empty folder and start tofu console, which evaluates expressions as you type them:
Terminal
tofu console
Type each expression to see the result. The last one compares text that looks like a number:
tofu console
> 3 < 5
true
> 5 <= 5
true
> 10 > 20
false
> 8080 >= 1024
true
> "10" > 9
true
> exit
Clean Up
Type exit to leave the console. The example creates nothing, so there’s nothing else to remove.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Comparing words with < or > | The operators expect numbers, so "tea" < "cake" stops with Invalid operand. | Compare words with == instead. |
Using > where the limit itself is allowed | The limit fails the check. | Use >= or <=. |
FAQ
Why was "10" > 9 true?
The docs say these operators expect numbers, and OpenTofu converted the text "10" to the number 10.
How do I check a range?
Combine two comparisons with &&: var.port >= 1024 && var.port <= 65535.
Conclusion
<, <=, > and >= compare two numbers and return true or false.
Additional Resources
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.
