source(3tcl)
π source β bestie, let's run a whole file like it's a script π
okay so this is the Tcl Built-In Commands manual, and today's main character is source, the little command that scoops up a file and makes Tcl run it. no cap, it's iconic.
β¨ NAME β¨
source β read a file or resource and evaluate it as a Tcl script. yes bestie, it's giving "load the whole vibe and execute it." π¦
πΈ SYNOPSIS πΈ
source fileName
source -encoding encodingName fileName
π DESCRIPTION π
so here's the tea: this command grabs the contents of the file or resource you point it at and hands the whole thing to the Tcl interpreter as a text script. π
what does it give back? the return value of source is literally the return value of the last command executed in that script. slay.
if an error happens while evaluating the contents of the script, then the source command will return that error. periodt, it doesn't hide it from you.
and if a return command gets invoked from inside the script, the rest of the file is skipped β like, we're done here β and the source command will return normally with the result from that return command. π
now the important lil details, don't skip these:
the end-of-file character for files is "\32" (that's ^Z) for all platforms. source will read files up to this character. this restriction does not exist for the read or gets commands, which is what allows for files containing code and data segments (scripted documents, iykyk). if you require a "^Z" in code for string comparison, you can use "\x1A", which will be safely substituted by the Tcl interpreter into "^Z". π
a leading BOM (Byte order mark) contained in the file is ignored for unicode encodings: utf-8, utf-16, ucs-2. lowkey thoughtful of it. π§
the -encoding option is used to specify the encoding of the data stored in fileName. when the -encoding option is omitted, the utf-8 encoding is assumed. no guessing games. π
π EXAMPLE π
run the script in the file foo.tcl and then the script in the file bar.tcl:
source foo.tcl
source bar.tcl
alternatively, because we love a loop moment:
foreach scriptFile {foo.tcl bar.tcl} {
source $scriptFile
}
π«Ά SEE ALSO π«Ά
π« KEYWORDS π«
file, script
π okay that's the whole page, bestie. every flag, every default, every ^Z, all of it. you're so welcome. π
this was brought to you by the Tcl manual, source(3tcl) β Tcl Built-In Commands, Tcl. mwah, go source something cute. πβ¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2000 Scriptics Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms