tclsh(1)
β¨ tclsh(1) β¨ β straight out of the Tcl Applications manual, section 1, bestie π«Ά
okay so picture this: you open your terminal, you type one little word, and suddenly you're inside a whole interpreter. that word is tclsh, and this page is literally the whole tea on it. no cap.
πΈ NAME πΈ
tclsh β a simple shell that's got a whole Tcl interpreter living inside it π
π SYNOPSIS π
tclsh ?-encoding name? ?fileName arg arg ...?
π DESCRIPTION π
tclsh is a shell-like application (yes, shell-like, it's giving shell but make it Tcl) that reads Tcl commands from its standard input or from a file, and then evaluates them. that's the whole job. iconic behavior honestly.
If you invoke it with no arguments at all, it runs interactively β reading Tcl commands from standard input and printing command results and error messages to standard output. it keeps going until the exit command gets invoked, or until it hits end-of-file on its standard input. either one, it's done. π¦
And here's the cute lil detail: if there's a file called .tclshrc in the user's home directory (or tclshrc.tcl on Windows platforms, hi windows besties π), then interactive tclsh evaluates that file as a Tcl script just before reading the very first command from standard input. like it's warming up. main character preparation energy.
π SCRIPT FILES π
If tclsh is invoked with arguments, then the first few arguments specify the name of a script file and, optionally, the encoding of the text data stored in that script file. any additional arguments after that get made available to the script as variables (peep the VARIABLES section below, i got you). π
So instead of reading commands from standard input, tclsh reads Tcl commands from the named file, and it exits when it reaches the end of that file. simple. clean. slay.
Now, how does it know it's at the end? the end of the file may be marked either by the physical end of the medium, or by the character "\032" ("\u001a", control-Z). if that character is present in the file, the tclsh application will read text up to but not including the character. and if your application genuinely needs that character inside the file? you can safely encode it as "\032", "\x1A", or "\u001a" β or you can generate it using commands like format or binary. options, bestie. we love options. β¨
heads up: there is no automatic evaluation of.tclshrcwhen the name of a script file is presented on thetclshcommand line. but no stress β the script file can alwayssourceit if it wants to. it's a choice, not a vibe. π
If you create a Tcl script in a file whose first line is
#!/usr/local/bin/tclsh
then you can invoke the script file directly from your shell if you mark the file as executable. cute! π this assumes tclsh has been installed in the default location in /usr/local/bin; if it lives somewhere else then you'll have to modify that line to match. and listen: many UNIX systems do not allow the #! line to exceed about 30 characters in length, so be sure the tclsh executable can be accessed with a short file name. yes this is a real constraint. yes it's giving 1970s. we move.
An even better approach (the glow-up approach, fr fr) is to start your script files with these three lines:
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" ${1+"$@"}
this approach has three advantages over the approach in the previous paragraph:
- the location of the
tclshbinary does not have to be hard-wired into the script β it can be anywhere in your shell search path. freedom!! π¦ - it gets around the 30-character file name limit from the previous approach. bye felicia.
- it will work even if
tclshis itself a shell script. (this is done on some systems in order to handle multiple architectures or operating systems: thetclshscript selects one of several binaries to run.) multi-arch? she contains multitudes.
so how does this three-line magic actually work? the three lines cause both sh and tclsh to process the script, but the exec is only executed by sh. sh processes the script first; it treats the second line as a comment and executes the third line. the exec statement causes the shell to stop processing and instead to start up tclsh to reprocess the entire script. and when tclsh starts up, it treats all three lines as comments, since the backslash at the end of the second line causes the third line to be treated as part of the comment on the second line. it's a little relay race and honestly? obsessed. πβοΈπ¨
one more thing you should note: it is also common practice to install tclsh with its version number as part of the name. the advantage is that it allows multiple versions of Tcl to exist on the same system at once. the disadvantage is that it makes it harder to write scripts that start up uniformly across different versions of Tcl. trade-offs, bestie. always trade-offs. π
π VARIABLES π
tclsh sets the following global Tcl variables, in addition to those created by the Tcl library itself (such as env, which maps environment variables such as PATH into Tcl):
argcβ contains a count of the number of arg arguments (0 if none), not including the name of the script file.argvβ contains a Tcl list whose elements are the arg arguments, in order, or an empty string if there are no arg arguments.argv0β contains fileName if it was specified. otherwise, it contains the name by whichtclshwas invoked.tcl_interactiveβ contains1iftclshis running interactively (no fileName was specified and standard input is a terminal-like device),0otherwise.
π PROMPTS π
When tclsh is invoked interactively it normally prompts for each command with "% ". you can change that prompt by setting the global variables tcl_prompt1 and tcl_prompt2. π
if the variable tcl_prompt1 exists then it must consist of a Tcl script to output a prompt; instead of outputting a prompt, tclsh will evaluate the script in tcl_prompt1. and the variable tcl_prompt2 is used in a similar way when a newline is typed but the current command is not yet complete. if tcl_prompt2 is not set then no prompt is output for incomplete commands. (so if you're wondering why your multi-line command gives you silence β that's the why, iykyk π)
π§ STANDARD CHANNELS π§
See Tcl_StandardChannels for more explanations. (go read it, it's right there, it's got the details)
π SEE ALSO π
β KEYWORDS β
application, argument, interpreter, prompt, script file, shell
and that's the page, bestie!! this whole thing is from the Tcl Applications manual, section 1 β so when you see tclsh(1) out in the wild, you'll know exactly who she is. Tcl, tclsh(1), periodt. go write something iconic πβ¨
π¦ where this page comes from
- fedora 43: tcl8 1:8.6.16-2.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 43: tcl8 1:8.6.16-2.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl8 1:8.6.17-2.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl8 1:8.6.17-2.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms