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

后端 未结 3 1950
情歌与酒
情歌与酒 2021-02-05 03:01

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

3条回答
  •  無奈伤痛
    2021-02-05 03:37

    You can use the $? variable, check out the bash documentation for this, it stores the exit status of the last command.

    Also, you might want to check out the bracket-style command blocks of bash (e.g. comm1 && (comm2 || comm3) && comm4), they are always executed in a subshell thus not altering the current environment, and are more powerful as well!

    EDIT: For instance, when using ()-style blocks as compared to bash -c 'command', you don't have to worry about escaping any argument strings with spaces, or any other special shell syntax. You directly use the shell syntax, it's a normal part of the rest of the code.

提交回复
热议问题