Is it possible to get the exit code from a subshell?

Geo picture Geo · Mar 31, 2010 · Viewed 31.3k times · Source

Let's imagine I have a bash script, where I call this:

bash -c "some_command"
do something with code of some_command here

Is it possible to obtain the code of some_command? I'm not executing some_command directly in the shell running the script because I don't want to alter it's environment.

Answer

Matti Virkkunen picture Matti Virkkunen · Mar 31, 2010

$? will contain the return code of some_command just as usual.

Of course it might also contain a code from bash, in case something went wrong before your command could even be executed (wrong filename, whatnot).