TCL – The difference between the eq and == operators

TCL Background

Unlike many programming languages (such as Python) TCL stores all values as strings. By using operators or commands these values can be interpreted as different data types (such as integers, binary etc).

‘eq’ vs ‘==’

Though, on the surface, the 2 TCL operators ‘eq’ and ‘==’ appear similar, ie  both return a boolean (i.e True or False) should the 2 values match. In fact they work extremely differently.
The difference between these 2 operators is that ‘eq’ is a string based operator, were as ‘==’ is numerical based. Meaning that, as all values within TCL are strings, when using ‘==’ both strings are converted to numbers.

Does it Matter ?

Firstly, extra clock cycles are required to convert each string to a number.
Secondly the numerical representation of the (string) isn’t always as clear as you may think. Let me show you what I mean.

Below shows you some examples of how TCL coerces the data into a number,

12 = 12
012 = 10 (any string containing a 0 at the beginning is classed as a base8 number during conversation)
012.0 = 12.0 (any string with a decimal point is classed as a decimal number during conversion, even though it starts with a 0).

As you can see, the string is converted into either an integer, a decimal or base8 (depending on the format of the string).

Reference (TCL / Everything is a String)

Rick Donato

Want to become a programming expert?

Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial