Script substitution

Script Substitution is the more apt term for Command Substitution, since entire scripts, not just individual commands, can be contained in brackets. The result of the script is the result of the final command. A new stack frame is not created, so using return or break or the like will cause the caller to return, etc.


PYK 2019-02-12: Script substitution can be recursive. Everything happens at the same level, as no additional levles are created:

puts [
    set n 10
    set a {
        puts $n
        expr {[incr n -1]? [try $a] : {liftoff}}
    try $a


AMG PYK: Script substitution can be used in combination with list and {*} to embed comments in lists. The syntax is awkward, but it does work:

switch -regexp $input [list {*}[
# Handle words starting with a capital letter
] {^[A-Z]} {
    # Do the thing
} {*}[
# Handle everything else
] default {
    # Don't do the thing

Though also consider using scripted list or decomment for this same purpose.

Long Strings

PYK 2020-11-13: Substitution of an empty script can be used break up long strings in a script:

set url /path/to/some/[