1subst(n) Tcl Built-In Commands subst(n)
2
3
4
5______________________________________________________________________________
6
8 subst - Perform backslash, command, and variable substitutions
9
11 subst ?-nobackslashes? ?-nocommands? ?-novariables? string
12_________________________________________________________________
13
15 This command performs variable substitutions, command substitutions,
16 and backslash substitutions on its string argument and returns the
17 fully-substituted result. The substitutions are performed in exactly
18 the same way as for Tcl commands. As a result, the string argument is
19 actually substituted twice, once by the Tcl parser in the usual fashion
20 for Tcl commands, and again by the subst command.
21
22 If any of the -nobackslashes, -nocommands, or -novariables are speci‐
23 fied, then the corresponding substitutions are not performed. For
24 example, if -nocommands is specified, command substitution is not per‐
25 formed: open and close brackets are treated as ordinary characters
26 with no special interpretation.
27
28 Note that the substitution of one kind can include substitution of
29 other kinds. For example, even when the -novariables option is speci‐
30 fied, command substitution is performed without restriction. This
31 means that any variable substitution necessary to complete the command
32 substitution will still take place. Likewise, any command substitution
33 necessary to complete a variable substitution will take place, even
34 when -nocommands is specified. See the EXAMPLES below.
35
36 If an error occurs during substitution, then subst will return that
37 error. If a break exception occurs during command or variable substi‐
38 tution, the result of the whole substitution will be the string (as
39 substituted) up to the start of the substitution that raised the excep‐
40 tion. If a continue exception occurs during the evaluation of a com‐
41 mand or variable substitution, an empty string will be substituted for
42 that entire command or variable substitution (as long as it is well-
43 formed Tcl.) If a return exception occurs, or any other return code is
44 returned during command or variable substitution, then the returned
45 value is substituted for that substitution. See the EXAMPLES below.
46 In this way, all exceptional return codes are “caught” by subst. The
47 subst command itself will either return an error, or will complete suc‐
48 cessfully.
49
51 When it performs its substitutions, subst does not give any special
52 treatment to double quotes or curly braces (except within command sub‐
53 stitutions) so the script
54 set a 44
55 subst {xyz {$a}}
56 returns “xyz {44}”, not “xyz {$a}” and the script
57 set a "p\} q \{r"
58 subst {xyz {$a}}
59 returns “xyz {p} q {r}”, not “xyz {p\} q \{r}”.
60
61 When command substitution is performed, it includes any variable sub‐
62 stitution necessary to evaluate the script.
63 set a 44
64 subst -novariables {$a [format $a]}
65 returns “$a 44”, not “$a $a”. Similarly, when variable substitution is
66 performed, it includes any command substitution necessary to retrieve
67 the value of the variable.
68 proc b {} {return c}
69 array set a {c c [b] tricky}
70 subst -nocommands {[b] $a([b])}
71 returns “[b] c”, not “[b] tricky”.
72
73 The continue and break exceptions allow command substitutions to pre‐
74 vent substitution of the rest of the command substitution and the rest
75 of string respectively, giving script authors more options when pro‐
76 cessing text using subst. For example, the script
77 subst {abc,[break],def}
78 returns “abc,”, not “abc,,def” and the script
79 subst {abc,[continue;expr {1+2}],def}
80 returns “abc,,def”, not “abc,3,def”.
81
82 Other exceptional return codes substitute the returned value
83 subst {abc,[return foo;expr {1+2}],def}
84 returns “abc,foo,def”, not “abc,3,def” and
85 subst {abc,[return -code 10 foo;expr {1+2}],def}
86 also returns “abc,foo,def”, not “abc,3,def”.
87
89 Tcl(n), eval(n), break(n), continue(n)
90
92 backslash substitution, command substitution, variable substitution
93
94
95
96Tcl 7.4 subst(n)