For reference: this works correctly under classic AmigaOS.
I tested it on 1.3, 2.1, 3.1 (ARexx 1.15) and 3.2.3, and all of them return the correct RC when running RX 42.rexx with a simple exit 42.
So the incorrect return code seems to be specific to the OS4 RX interpreter.
When you posted this back in June, I tried to reproduce it, but at first it seemed to work for me. Then I got sidetracked and forgot to do more investigation, sorry.
Now I tried some more, and I think I found a way to reproduce it.
Could you try repeating your test, but in the third step, instead of typing "RX 42.rexx", just type "RX 42".
Does that work correctly?
That's what I found here at least.
If you can confirm, I'll try making a bug report about it. Not sure which component exactly is at fault, though, nor when/if it can be fixed at all in the old ARexx code, but at least we will know the workaround: Omit the .rexx suffix when invoking an ARexx script file (it is optional after all). Yes, I know, crude, but still ...
Oh, and @FlynnTheAvatar: I would be curious to know whether the same difference is present in the classic version. Your comment above seems to indicate it's not, but could you please verify again?
About this last observation: I've had this explained to me by a developer, and it makes sense to me now.
The thing is, when you explicitly invoke a script, it's the Rx command itself that tries to load the script, and it has been coded to know to look for the script on "the ARexx path" (i.e. including in the REXX: assign) with or without the .rexx suffix being given.
However, when you omit the Rx command and just type the script name, it's the Shell that looks for the file, and it cannot guess that you are referring to an ARexx script, so it has to find it with the exact name. Only after checking inside it can it see that it starts with a "/* */" comment, so it will know to submit it for running as an ARexx script. (There are similar rules for scripts with "#! {command}" ("hash-bang") at the start (pass script to that command) and with "#!python" (run as Python program), but those do not require specific file name suffices, so less relevant here).
In short, the behaviour I described above is not a bug, it's just a necessary consequence of the various components at play in the two cases.
As for the original issue with the return codes, it looks to me like it ought to work the same in OS4 as in OS3, and I will try to raise an issue in our bugtracker about it. As usual, no promises, but at least it's probably something related to the OS4 version of the Rx command itself, not the actual ARexx language, (which would be much more unlikely to be fixed).
Side note: Remember that ARexx actually considers such return values as text values, and the conversion to an integer return code (if possible) happens or should happen on returning to the calling (Shell) environment.
Interesting experiments can be made if you try "exit hello" instead of "exit 42", and doing it from a called external ARexx function (a separate script) versus from a script called directly from Rx as we did above.
Also check out the ARexx documentation for Exit versus Return before doing those experiments.