tclsh(1)

πŸ’– NAME πŸ’–

okay so bestie, welcome to tclsh β€” it's a simple shell containing a Tcl interpreter. that's the whole vibe. a shell. with Tcl in it. iconic.


✨ SYNOPSIS ✨

tclsh ?-encoding name? ?fileName arg arg ...?

πŸ¦‹ DESCRIPTION πŸ¦‹

tclsh is a shell-like application that reads Tcl commands from its standard input or from a file, and then evaluates them. literally that's the job. that's the whole job. πŸ’…

If you invoke it with no arguments at all, it runs interactively: it reads Tcl commands from standard input and prints command results and error messages to standard output. It keeps going until you invoke the exit command, or until it reaches end-of-file on its standard input. whichever comes first, no cap.

Also!! if there's a file called .tclshrc (or tclshrc.tcl on the Windows platforms) hanging out in the user's home directory, then an interactive tclsh evaluates that file as a Tcl script just before it reads the first command from standard input. it's like a little getting-ready routine before the main character enters. ✨


πŸŽ€ 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 get made available to the script as variables (peep the VARIABLES section below, bestie 🫢).

So instead of reading commands from standard input, tclsh will read Tcl commands from the named file, and tclsh will exit when it reaches the end of that file.

Now, how do you know where the end of the file is? Two ways, and one of them is lowkey unhinged:

  • the physical end of the medium, or
  • the character \x1A (control-Z) πŸ‘€

If this character is present in the file, the tclsh application will read text up to but not including that character. An application that requires this character in the file may safely encode it as \x1A; or may generate it by use of commands such as format or binary. so you're not stuck, it's giving solvable.

heads up: there is no automatic evaluation of .tclshrc when the name of a script file is presented on the tclsh command line. but the script file can always source it if it wants to. its choice. its moment. ✨

πŸͺ„ the shebang situation

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. slay. πŸŽ€

But β€” and this is important β€” this assumes that tclsh has been installed in the default location in /usr/local/bin. If it's installed somewhere else, you'll have to modify that line to match. no guessing, fr fr.

Also, 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. 30 characters. that's it. that's the budget. 😭

πŸ‘‘ the even better approach

An even better approach 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, and I'm gonna list all three because they ate:

  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 in the previous approach. bye budget. πŸ’…
  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.) multitasking queen.

The three lines cause both sh and tclsh to process the script, but the exec is only executed by sh. Here's the play-by-play:

  • 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.
  • 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 two-girl relay race and they both win. 🏁

🌸 version-number names

You should note that it's also common practice to install tclsh with its version number as part of the name. This has the advantage of allowing multiple versions of Tcl to exist on the same system at once, but also the disadvantage of making it harder to write scripts that start up uniformly across different versions of Tcl. tradeoffs, bestie. always tradeoffs. βš–οΈ

🌈 the env route

Alternatively, you can use /usr/bin/env to locate tclsh on the path, like this:

#!/usr/bin/env tclsh

That has the advantages of being succinct and simple, but the disadvantage of not being flexible in the face of varying names for the binary. pick your fighter. πŸ₯Š


⭐ 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). so there's the library's stuff, and then there's her stuff:

  • 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, 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 % . cute, classic, timeless.

You can change the 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.
  • 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 yes you can have a completely custom lil prompt moment. main character energy. πŸ‘‘


🌈 STANDARD CHANNELS 🌈

See Tcl_StandardChannels for more explanations. that's where the lore lives. 🫢


🧚 ZIPFS VIRTUAL FILE SYSTEM 🧚

okay this part is genuinely so cool. when a zipfile is concatenated to the end of a tclsh, on startup the contents of the zip archive will be mounted under a virtual file system (VFS). the root of that VFS can be retrieved using the zipfs root command.

The zip archive is mounted under the app directory within the VFS.

  • If a file named main.tcl is present in the top level directory of the zip archive, it will be sourced instead of tclsh's normal command line handing. πŸŽ€
  • If a top level directory tcl_library is present in the zip archive, it will become the directory loaded as env(TCL_LIBRARY).
  • And β€” because she really wants you to get it β€” if a file named main.tcl is present in the top level directory of the zip archive, it will be sourced instead of the shell's normal command line handling. (yes she said it twice. she's thorough. πŸ’…)

Only one zipfile can be concatenated to the end of an executable image (tclsh, or wish). However, if multiple zipfiles are concatenated, only the last one is used. so don't stack them thinking you're clever. πŸ’€

This filesystem is read-only. Files cannot be added or modified within this mounted file system. no touchy. πŸ™ˆ

See zipfs(3tcl) for complete details.


πŸ‘‘ SEE ALSO πŸ‘‘


πŸ“ KEYWORDS πŸ“

application, argument, interpreter, prompt, script file, shell, zipfs


and that's the whole page, bestie!! 🌈 this one came from the Tcl crew β€” it's the tclsh(1) manual, and the footer was just the word Tcl sitting pretty down there at the bottom with the page name. now go write a script with a shebang and feel like the moment. πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from