Version 4 of [

Updated 2020-07-08 03:41:32 by AMG

[ is a syntactical element of Tcl that directs the interpreter to perform command substitution.

It also happens to be impossible to search for this page or to link to it from any other wiki page :)

AMG: Link to it as http://wiki.tcl.tk/40574 or [L1 ] or [ ..

Line continuation

AMG: [ is part of an alternative line continuation syntax that does not insert a space. If the script substitution (i.e. all the text between [ and ]) consists entirely of whitespace (including newline) and/or comment, it is replaced with empty string. This is useful when a very long string needs to be broken across multiple lines, yet the string must not have any spaces. It might also be handy to embed comments, though it's usually easier to just put the comments above the line of code.

For example:

puts this_is_a_really_long_string_that_[
        ]cannot_have_any_spaces_in_it
puts this_string_has_a_[
# comment!
        ]secret_in_the_middle

Prints:

this_is_a_really_long_string_that_cannot_have_any_spaces_in_it
this_string_has_a_secret_in_the_middle

Remember that comments terminate with newline (when not preceded by an odd number of backslashes), not with ]. Thus, this does not allow for single-line embedded comments. puts a[#comment]b fails because ]b are part of the comment, then the interpreter is left wanting a ] to close the script substitution.

Be mindful that this notation is only available in a command context, not when constructing a brace-quoted string. It does, however, work with double-quoted strings. The important thing is that the context allows script substitutions.

Unfortunately, this notation cannot be used to add embedded comments to expr. It evaluates to empty string, which is a value on which expr will want to operate.