| 1 |
'\" |
| 2 |
'\" Copyright (c) 1994 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2001 Donal K. Fellows |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH subst n 7.4 Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
subst \- Perform backslash, command, and variable substitutions |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBsubst \fR?\fB\-nobackslashes\fR? ?\fB\-nocommands\fR? ?\fB\-novariables\fR? \fIstring\fR |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
This command performs variable substitutions, command substitutions, |
| 21 |
and backslash substitutions on its \fIstring\fR argument and |
| 22 |
returns the fully-substituted result. |
| 23 |
The substitutions are performed in exactly the same way as for |
| 24 |
Tcl commands. |
| 25 |
As a result, the \fIstring\fR argument is actually substituted twice, |
| 26 |
once by the Tcl parser in the usual fashion for Tcl commands, and |
| 27 |
again by the \fIsubst\fR command. |
| 28 |
.PP |
| 29 |
If any of the \fB\-nobackslashes\fR, \fB\-nocommands\fR, or |
| 30 |
\fB\-novariables\fR are specified, then the corresponding substitutions |
| 31 |
are not performed. |
| 32 |
For example, if \fB\-nocommands\fR is specified, command substitution |
| 33 |
is not performed: open and close brackets are treated as ordinary characters |
| 34 |
with no special interpretation. |
| 35 |
.PP |
| 36 |
Note that the substitution of one kind can include substitution of |
| 37 |
other kinds. For example, even when the \fB\-novariables\fR option |
| 38 |
is specified, command substitution is performed without restriction. |
| 39 |
This means that any variable substitution necessary to complete the |
| 40 |
command substitution will still take place. Likewise, any command |
| 41 |
substitution necessary to complete a variable substitution will |
| 42 |
take place, even when \fB\-nocommands\fR is specified. See the |
| 43 |
\fBEXAMPLES\fR below. |
| 44 |
.PP |
| 45 |
If an error occurs during substitution, then \fBsubst\fR will return |
| 46 |
that error. If a break exception occurs during command or variable |
| 47 |
substitution, the result of the whole substitution will be the |
| 48 |
string (as substituted) up to the start of the substitution that |
| 49 |
raised the exception. If a continue exception occurs during the |
| 50 |
evaluation of a command or variable substitution, an empty string |
| 51 |
will be substituted for that entire command or variable substitution |
| 52 |
(as long as it is well-formed Tcl.) If a return exception occurs, |
| 53 |
or any other return code is returned during command or variable |
| 54 |
substitution, then the returned value is substituted for that |
| 55 |
substitution. See the \fBEXAMPLES\fR below. In this way, all exceptional |
| 56 |
return codes are |
| 57 |
.QW caught |
| 58 |
by \fBsubst\fR. The \fBsubst\fR command |
| 59 |
itself will either return an error, or will complete successfully. |
| 60 |
.SH EXAMPLES |
| 61 |
.PP |
| 62 |
When it performs its substitutions, \fIsubst\fR does not give any |
| 63 |
special treatment to double quotes or curly braces (except within |
| 64 |
command substitutions) so the script |
| 65 |
.PP |
| 66 |
.CS |
| 67 |
set a 44 |
| 68 |
\fBsubst\fR {xyz {$a}} |
| 69 |
.CE |
| 70 |
.PP |
| 71 |
returns |
| 72 |
.QW "\fBxyz {44}\fR" , |
| 73 |
not |
| 74 |
.QW "\fBxyz {$a}\fR" |
| 75 |
and the script |
| 76 |
.PP |
| 77 |
.CS |
| 78 |
set a "p\e} q \e{r" |
| 79 |
\fBsubst\fR {xyz {$a}} |
| 80 |
.CE |
| 81 |
.PP |
| 82 |
returns |
| 83 |
.QW "\fBxyz {p} q {r}\fR" , |
| 84 |
not |
| 85 |
.QW "\fBxyz {p\e} q \e{r}\fR". |
| 86 |
.PP |
| 87 |
When command substitution is performed, it includes any variable |
| 88 |
substitution necessary to evaluate the script. |
| 89 |
.PP |
| 90 |
.CS |
| 91 |
set a 44 |
| 92 |
\fBsubst\fR -novariables {$a [format $a]} |
| 93 |
.CE |
| 94 |
.PP |
| 95 |
returns |
| 96 |
.QW "\fB$a 44\fR" , |
| 97 |
not |
| 98 |
.QW "\fB$a $a\fR" . |
| 99 |
Similarly, when |
| 100 |
variable substitution is performed, it includes any command |
| 101 |
substitution necessary to retrieve the value of the variable. |
| 102 |
.PP |
| 103 |
.CS |
| 104 |
proc b {} {return c} |
| 105 |
array set a {c c [b] tricky} |
| 106 |
\fBsubst\fR -nocommands {[b] $a([b])} |
| 107 |
.CE |
| 108 |
.PP |
| 109 |
returns |
| 110 |
.QW "\fB[b] c\fR" , |
| 111 |
not |
| 112 |
.QW "\fB[b] tricky\fR" . |
| 113 |
.PP |
| 114 |
The continue and break exceptions allow command substitutions to |
| 115 |
prevent substitution of the rest of the command substitution and the |
| 116 |
rest of \fIstring\fR respectively, giving script authors more options |
| 117 |
when processing text using \fIsubst\fR. For example, the script |
| 118 |
.PP |
| 119 |
.CS |
| 120 |
\fBsubst\fR {abc,[break],def} |
| 121 |
.CE |
| 122 |
.PP |
| 123 |
returns |
| 124 |
.QW \fBabc,\fR , |
| 125 |
not |
| 126 |
.QW \fBabc,,def\fR |
| 127 |
and the script |
| 128 |
.PP |
| 129 |
.CS |
| 130 |
\fBsubst\fR {abc,[continue;expr {1+2}],def} |
| 131 |
.CE |
| 132 |
.PP |
| 133 |
returns |
| 134 |
.QW \fBabc,,def\fR , |
| 135 |
not |
| 136 |
.QW \fBabc,3,def\fR . |
| 137 |
.PP |
| 138 |
Other exceptional return codes substitute the returned value |
| 139 |
.PP |
| 140 |
.CS |
| 141 |
\fBsubst\fR {abc,[return foo;expr {1+2}],def} |
| 142 |
.CE |
| 143 |
.PP |
| 144 |
returns |
| 145 |
.QW \fBabc,foo,def\fR , |
| 146 |
not |
| 147 |
.QW \fBabc,3,def\fR |
| 148 |
and |
| 149 |
.PP |
| 150 |
.CS |
| 151 |
\fBsubst\fR {abc,[return -code 10 foo;expr {1+2}],def} |
| 152 |
.CE |
| 153 |
.PP |
| 154 |
also returns |
| 155 |
.QW \fBabc,foo,def\fR , |
| 156 |
not |
| 157 |
.QW \fBabc,3,def\fR . |
| 158 |
.SH "SEE ALSO" |
| 159 |
Tcl(n), eval(n), break(n), continue(n) |
| 160 |
.SH KEYWORDS |
| 161 |
backslash substitution, command substitution, quoting, substitution, variable substitution |
| 162 |
.\" Local Variables: |
| 163 |
.\" mode: nroff |
| 164 |
.\" End: |