Search code examples
forthgforth

How can I exit Forth with a non-zero exit status?


I would like to exit a Forth program (using Gforth 0.7.3) with a non-zero exit status.

I've tried:

1 bye

But the 1 is not interpreted as an argument to bye (and I didn't expect this to work anyway; I couldn't find any hint in the documentation that bye would accept an argument).

Note that I do not want to trigger an exception, as that also prints an error message (unless there is a way to suppress the error message of the exception from within the Forth program itself).

So, how do I exit a Forth program back to the hosted environment/OS providing a non-zero exit status? (Basically, I'm looking for the equivalent of return EXIT_FAILURE; // from main() (C) or exit(EXIT_FAILURE); (C) or System.exit(1); (Java).)


Solution

  • In Gforth, the internal word (bye) ( n -- ) can be used to return the exit status to the OS.

    For example, the following command in Bash:

    gforth -e '123 (bye)' ; echo $?
    

    prints "123".

    In general, a method to return a non-zero exit status is not standardized yet.