How to get the exit status a loop in bash

后端 未结 8 789
醉梦人生
醉梦人生 2020-12-16 15:38

I know how to check the status of the previously executed command using $?, and we can make that status using exit command. But for the loops in bash are always returning a

8条回答
  •  一向
    一向 (楼主)
    2020-12-16 16:21

    The bash manual says:

    while list-1; do list-2; done
    until list-1; do list-2; done
      [..]The exit status of the while and until commands is the exit status
      of the last command executed in list-2, or zero if none was executed.[..]
    

    The last command that is executed inside the loop is break. And the exit value of break is 0 (see: help break).

    This is why your program keeps exiting with 0.

提交回复
热议问题