Version 6 of [

Updated 2020-07-08 03:44:46 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 https://wiki.tcl-lang.org/page/%5B .

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 ]. Therefore, this does not allow for same-line embedded comments. "puts a[#comment]b" fails because ]b is part of the comment, leaving the interpreter wanting a ] to close the script substitution.

Be mindful that this notation is not available within brace-quoted strings. It does, however, work with double-quoted strings, and certainly with the list command. The important thing is that the context must allow 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.