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

后端 未结 3 1919
情歌与酒
情歌与酒 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:42

    Here's an illustration of $? and the parenthesis subshell mentioned by Paggas and Matti:

    $ (exit a); echo $?
    -bash: exit: a: numeric argument required
    255
    $ (exit 33); echo $?
    33
    

    In the first case, the code is a Bash error and in the second case it's the exit code of exit.

提交回复
热议问题