ZetCode

Tcl info Command

last modified October 20, 2026

The Tcl info command provides introspection capabilities about the Tcl interpreter state. It can query information about variables, procedures, commands, and the execution environment.

Basic Definition

The info command is a built-in Tcl command that returns information about various interpreter components. It has multiple subcommands for different types of information.

Syntax: info subcommand ?arg ...?. The behavior depends on the subcommand used. It can query variables (exists, vars, globals), procedures (procs, args, body), commands, the call stack (level), the running script (script) and the interpreter itself (tclversion, patchlevel).

Checking Command Existence

The info commands subcommand lists all available commands or checks if a specific command exists.

info_commands.tcl
# Check if 'puts' command exists
if {[info commands puts] eq "puts"} {
    puts "The puts command is available"
}

# List all commands matching a pattern
set tcl_commands [info commands tcl*]
puts "Tcl commands starting with 'tcl': $tcl_commands"

This example first checks if the puts command exists. Then it lists all commands starting with 'tcl'; the argument is a glob pattern, and without it all commands visible in the current namespace are listed. Here only tclLog matches. The info commands subcommand is useful for dynamic command checking.

Getting Variable Information

The info exists subcommand checks if a variable exists in the current scope.

info_exists.tcl
set name "John"

if {[info exists name]} {
    puts "Variable 'name' exists with value: $name"
} else {
    puts "Variable 'name' doesn't exist"
}

# Check for non-existent variable
if {![info exists age]} {
    puts "Variable 'age' doesn't exist"
}

This demonstrates checking variable existence before using it. The info exists command returns 1 if the variable exists, 0 otherwise. Reading a missing variable raises an error, so checking first is the safe way to access a variable that may not be set. For an array element, use info exists arr(key).

Procedure Information

The info procs subcommand lists procedures and info body returns a procedure's implementation.

info_procs.tcl
proc greet {name} {
    return "Hello, $name"
}

# List all procedures
set all_procs [info procs]
puts "All procedures: $all_procs"

# Get procedure body
set greet_body [info body greet]
puts "Greet procedure body: $greet_body"

This shows how to list all procedures and examine a procedure's body. The list also contains the procedures that Tcl's own library defines (such as auto_load), and not the built-in commands like puts, which are not procedures. The body is returned exactly as written, including the leading and trailing newline. The info procs command is useful for debugging and dynamic programming.

Script Information

The info script subcommand returns the name of the currently executing script file.

info_script.tcl
puts "This script is: [info script]"

proc show_script {} {
    puts "Called from script: [info script]"
}

show_script

This demonstrates getting the script filename in both the main script and a procedure. info script returns an empty string if not executing from a file. It reports the file that is being sourced when it is called, not the file in which a procedure was defined, so it is best called at the top level of a script.

Execution Level Information

The info level subcommand returns information about the call stack.

info_level.tcl
proc outer {} {
    puts "Outer level: [info level]"
    inner
}

proc inner {} {
    puts "Inner level: [info level]"
    puts "Caller is: [info level -1]"
}

outer

This shows the nesting level of procedure calls. info level returns the current depth, while info level -1 returns the caller's command.

Tcl Version Information

The info tclversion subcommand returns the Tcl interpreter version.

info_tclversion.tcl
puts "Running Tcl version: [info tclversion]"

if {[package vcompare [info tclversion] 8.6] >= 0} {
    puts "This is Tcl 8.6 or newer"
} else {
    puts "This is older than Tcl 8.6"
}

This checks the Tcl version and performs version comparison. The info tclversion command returns only major.minor (8.6); info patchlevel returns the full version, such as 8.6.12. Both are useful for writing version-dependent code.

Best Practices

This tutorial covered the Tcl info command with practical examples showing its various subcommands and uses.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.