Environment and RETURN-CODE
RETURN-CODE
RETURN-CODE is a special register that holds the exit status. Move a value into it before STOP RUN, and the shell sees it as the program exit code. Save this as exit8.cob:
MOVE 8 TO RETURN-CODE.
STOP RUN.
cobc -x -free -o exit8 exit8.cob
./exit8; echo $?
8
Special registers
Other special registers: COMMAND-LINE holds the raw command line, LINE-COUNTER and PAGE-COUNTER track report position, and TALLYING registers count INSPECT operations. They read like variables but live in the run unit.
Shell integration
Test the exit code in a shell script to chain batch jobs. Zero means success, and any other value stops the chain, which is how mainframe job schedulers and Unix makefiles alike decide what runs next.
The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev