Under Lubuntu 18.04 and Octave 4.2.2, this script
#!/usr/bin/octave
exit(1);
executed from the command line with
$ ./exitscript.m
always returns this output
terminate called after throwing an instance of 'octave::exit_exception'
panic: Aborted -- stopping myself...
attempting to save variables to 'octave-workspace'...
save to 'octave-workspace' complete
octave exited with signal 6
and prompting for exit code (right after executing the command)
$ echo $?
always returns 0, ignoring the value passed to the system.
Related bug report.
That looks like a bug in Octave 4.2. exit
shouldn't crash. (Surprise!)
Looks like it's been fixed in newer versions of Octave, starting with Octave 4.4.0. I think you need to just upgrade, here.
Unfortunately, most Linux distros ship with quite old versions of Octave. To get a more recent Octave version, you can install a 4.4.x or 5.1.x Octave using Flatpak or build it from source.