Get previous exit code ($?)

Given:

$ command
$ cargo run

Is there a way for the rust code to get the exit code of the command? that is to read $? on linux/macOs and %ERROR_LEVEL% on Windows?

Probably not. The $? variable is not a real environment variable. It's just something special where bash inserts the exit code of the previous command, and bash knows the exit code because bash was the process that started command. You'll have to convince bash to tell the Rust program what the exit code was.

3 Likes