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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.