Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 938
Geo Avatar asked Mar 31 '10 20:03

Geo


People also ask

How do you exit a subshell?

Use signals to exit process from subshell This is the mechanism that allows you to exit a command line process by pressing ctrl-c. When you press ctrl-c in your terminal, an interrupt signal (SIGINT) is sent to the current process.

How do you find exit value?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.

How do I find exit code in terminal?

Launch a terminal, and run any command. Check the value of the shell variable “$?” for the exit code. $ echo $? As the “date” command ran successfully, the exit code is 0.

How do I exit shell script without exiting shell?

Use return . The return bash builtin will exit the sourced script without stopping the calling (parent/sourcing) script. Causes a function to stop executing and return the value specified by n to its caller.


1 Answers

$? 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).

like image 64
Matti Virkkunen Avatar answered Oct 09 '22 05:10

Matti Virkkunen