exit

exit , a built-in routine, terminates the current process and reports its exit status to the calling process.

Synopsis

exit ?returnCode?

Description

exit terminates the entire process, not just the interpreter it is is executed from. returnCode, which defaults to 0, is the exit status to report. . A non-zero exit status is usually interpreted as an error case by the calling process, the exit command useful for signaling that the process did not complete normally.

Discussion

Takes an optional return code (small integer, 0..127) as argument.

LV does exit itself limit the return value to 0-127, or is it the errorCode Magic Name that is doing the limiting?

From comp.lang.tcl:

Andreas Otto reported "exit 999" to return 231 as a bug.

Pat Thoyts: I notice that 999 is 0x03e7 and 231 is 0x00e7 so provided you restrict exit codes to a maximum of 255 everything should be ok.

Donal Fellows: However it is not a good idea to use exit codes larger than 127, as 128 and higher are used to represent exits due to (unhandled) signals on UNIX. Mind you, if you need more than 128 different exit codes, you've probably got the wrong architecture of software... :^)


Oh, come on:

rename exit __exit
proc exit args {
    set code [ lindex $args 0 ]
    if {$code == 12345678910111213} { __exit }
    puts stderr {you and what army?}
}

It's Tcl guys, remember??


I also noticed that exit is being called when the program ends even if you do not call exit. this can be useful if you want a certain event happen before the exit:

rename exit __exit
proc exit args {
    callCloseEvents
    __exit {*}$args
}

RS observes, first on Linux - negatives are taken modulo 256:

 suchenwi@smart2[~]508: echo "exit -1" | tclsh; echo $?
 255
 suchenwi@smart2[~]509: echo "exit -2" | tclsh; echo $?
 254

Then on Windows 2000 (Cygwin) - negatives are set to 0, positives are taken mod 256:

 K_Inst2000@KSTBW798[/etc/pbc/1030]559: echo "exit -1" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]560: echo "exit" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]561: echo "exit 42" | tclsh; echo $?
 42
 K_Inst2000@KSTBW798[/etc/pbc/1030]562: echo "exit 512" | tclsh; echo $?
 0
 K_Inst2000@KSTBW798[/etc/pbc/1030]563: echo "exit 511" | tclsh; echo $?
 255

Here's how to redirect exit in slave interpreters, so that they just terminate themselves, but not the whole app:

interp alias $slave exit {} interp delete $slave