ZetCode

Tcl exit Command

last modified October 20, 2026

The Tcl exit command is used to terminate a Tcl script or application. It immediately stops script execution and returns control to the operating system. Buffered output on open channels, including standard output, is flushed before the process ends.

Basic Definition

The exit command terminates the Tcl interpreter with an optional exit code. By default, it returns 0 indicating successful termination.

Syntax: exit ?returnCode?. The optional returnCode is an integer that indicates the exit status to the calling process. On Unix only the low 8 bits are kept, so valid values are 0 to 255.

Basic Exit Command

This shows the simplest usage of exit to terminate a script.

basic_exit.tcl
puts "Starting script..."
exit
puts "This line will never be executed"

The script prints the first message and then terminates. The second message after exit is never reached. This demonstrates the immediate nature of the command.

Exit with Status Code

The exit command can return a status code to the calling process.

exit_status.tcl
set success 0
set failure 1

if {[file exists "data.txt"]} {
    puts "File exists"
    exit $success
} else {
    puts "File not found"
    exit $failure
}

This script checks for a file's existence and exits with different status codes. Status 0 typically means success, while non-zero indicates an error condition.

Conditional Exit

exit can be used with conditional statements to control program flow.

conditional_exit.tcl
set input [gets stdin]

if {$input eq "quit"} {
    puts "Exiting program..."
    exit
}

puts "You entered: $input"

This script reads user input and exits if the input is "quit". Otherwise, it echoes the input back. This pattern is common in interactive programs. With an empty standard input, as in this run, gets returns an empty string and the script ends normally; a real loop would also test for end-of-file.

Exit in Procedures

The exit command can be called from within Tcl procedures.

exit_proc.tcl
proc check_args {args} {
    if {[llength $args] == 0} {
        puts "Error: No arguments provided"
        exit 1
    }
    puts "Processing arguments..."
}

check_args
puts "This won't be printed"

This demonstrates how exit terminates the entire script, even when called from a procedure. The procedure checks for arguments and exits if none are provided. The exit status of this run is 1.

Exit with Error Message

Combine exit with error messages for better diagnostics.

exit_error.tcl
set required_version 8.6

if {[info tclversion] < $required_version} {
    puts stderr "Error: Requires Tcl $required_version or higher"
    exit 1
}

puts "Running with Tcl [info tclversion]"

This script checks the Tcl version and exits with an error message if the version is too old. The message is printed to stderr for proper error handling. Comparing versions with < is fragile, because it compares numbers (8.10 would be less than 8.6); package vcompare compares version strings correctly.

Graceful Shutdown

Use exit with cleanup operations for graceful termination.

exit_cleanup.tcl
proc cleanup {} {
    puts "Performing cleanup operations..."
    # Close files, release resources, etc.
}

proc main {} {
    puts "Starting main operations..."
    after 1000
    cleanup
    exit 0
}

main

This example shows a pattern for graceful shutdown. The cleanup procedure is called before exit to ensure proper resource management. Note that exit ends the process at once: it does not unwind the call stack, so finally clauses of enclosing try commands are not run, and cleanup has to be done before calling it.

Best Practices

This tutorial covered the Tcl exit command with practical examples showing its usage in different scenarios.

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.