You need the braces. Furthermore, you don't want the []
. []
evaluates its contents as a command, and replaces itself with the return value of the command. So your first version
if [$step > 0] { }
assuming $step
is 3
, is equivalent to
if [3 > 0] { }
which tries to evaluate [3 > 0]
as a command and complains that 3
is not a command.
Your second version has the same problem, because the expression evaluates any embedded command substitutions.
What you really want is just
if {$step > 0} { ... }
The thing to remember is that in Tcl, control structures behave the same way as user-defined functions (and in fact, can typically be implemented using Tcl code). The if
command takes 2-4 arguments, of the form "if" condition then-block [["else"] else-block]
. The first argument is always the condition. The second is the then-block. The third is the optional word "else", followed by the 4th which is an optional else-block (if the "else" word is present, the else-block is required).
Since this construct takes simple arguments, you need to be sure to provide it arguments. So the condition argument needs to be braced, so as to ensure it's a single argument (and also, so as to ensure any substitutions aren't interpreted too soon, as the evaluation of the condition will interpret them for you). Similarly the then-block and else-block are braced.