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