tclsh(1)

πŸ“¦ version 8.6.17, in fedora 43, fedora 44

✨ 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 .tclshrc when the name of a script file is presented on the tclsh command line. but no stress β€” the script file can always source it 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:

  1. the location of the tclsh binary does not have to be hard-wired into the script β€” it can be anywhere in your shell search path. freedom!! πŸ¦‹
  2. it gets around the 30-character file name limit from the previous approach. bye felicia.
  3. it will work even if tclsh is itself a shell script. (this is done on some systems in order to handle multiple architectures or operating systems: the tclsh script 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 which tclsh was invoked.
  • tcl_interactive β€” contains 1 if tclsh is running interactively (no fileName was specified and standard input is a terminal-like device), 0 otherwise.

πŸ“ 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