Bash: forcing the process exit code using a subshell

If the logic in your Bash script needs to force the last exit code variable “$?”, you can accomplish that with an exit inside a subprocess.

For example, here is a script snippet.

$(exit 99)
echo "last process exit code was $?"

This would return the output below.

last process exit code was 99

This is a very contrived example, but if you find yourself needing to execute a series of commands that ultimately gets tested by $?, then forcing the exit code can be handy.

REFERENCES

stackoverflow, set the return code in bash