Artifact bb20c80f4202aa6e3b2f75b6f5d0879573550ad1da3eed350ac8f4b332fb5bde:
- File doc/markdown/unknown.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 4029) [annotate] [blame] [check-ins using]
CommandName: unknown ManualSection: n Version: unknown TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- info(n)
- proc(n)
- interp(n)
- library(n)
- namespace(n) Keywords:
- error
- non-existent command
- unknown Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-1996 Sun Microsystems, Inc. ---
Name
unknown - Handle attempts to use non-existent commands
Synopsis
::: {.synopsis} ::: [unknown]{.cmd} [cmdName]{.arg} [arg arg]{.optdot} :::
Description
This command is invoked by the Tcl interpreter whenever a script tries to invoke a command that does not exist. The default implementation of fBunknownfR is a library procedure defined when Tcl initializes an interpreter. You can override the default fBunknownfR to change its functionality, or you can register a new handler for individual namespaces using the fBnamespace unknownfR command. Note that there is no default implementation of fBunknownfR in a safe interpreter.
If the Tcl interpreter encounters a command name for which there is not a defined command (in either the current namespace, or the global namespace), then Tcl checks for the existence of an unknown handler for the current namespace. By default, this handler is a command named fB::unknownfR. If there is no such command, then the interpreter returns an error. If the fBunknownfR command exists (or a new handler has been registered for the current namespace), then it is invoked with arguments consisting of the fully-substituted name and arguments for the original non-existent command. The fBunknownfR command typically does things like searching through library directories for a command procedure with the name fIcmdNamefR, or expanding abbreviated command names to full-length, or automatically executing unknown commands as sub-processes. In some cases (such as expanding abbreviations) fBunknownfR will change the original command slightly and then (re-)execute it. The result of the fBunknownfR command is used as the result for the original non-existent command.
The default implementation of fBunknownfR behaves as follows. It first calls the fBauto_loadfR library procedure to load the command. If this succeeds, then it executes the original command with its original arguments. If the auto-load fails and Tcl is run interactively then fBunknownfR calls fBauto_execokfR to see if there is an executable file by the name fIcmdfR. If so, it invokes the Tcl fBexecfR command with fIcmdfR and all the fIargsfR as arguments. If fIcmdfR cannot be auto-executed, fBunknownfR checks to see if the command was invoked at top-level and outside of any script. If so, then fBunknownfR takes two additional steps. First, it sees if fIcmdfR has one of the following three forms: fB!!fR, fB!fRfIeventfR, or fB^fRfIoldfRfB^fRfInewfR?fB^fR?. If so, then fBunknownfR carries out history substitution in the same way that fBcshfR would for these constructs. Finally, fBunknownfR checks to see if fIcmdfR is a unique abbreviation for an existing Tcl command. If so, it expands the command name and executes the command with the original arguments. If none of the above efforts has been able to execute the command, fBunknownfR generates an error return. If the global variable fBauto_noloadfR is defined, then the auto-load step is skipped. If the global variable fBauto_noexecfR is defined then the auto-exec step is skipped. Under normal circumstances the return value from fBunknownfR is the return value from the command that was eventually executed.
Example
Arrange for the fBunknownfR command to have its standard behavior except for first logging the fact that a command was not found:
# Save the original one so we can chain to it
rename unknown _original_unknown
# Provide our own implementation
proc unknown args {
puts stderr "WARNING: unknown command: $args"
uplevel 1 [list _original_unknown {*}$args]
}