How to get the exit status a loop in bash

后端 未结 8 796
醉梦人生
醉梦人生 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:09

    I would like to submit an alternative solution which is simpler and I think more elegant:

    (while true
     do
        if [ -f "test" ] ; then
            break
        fi  
     done
    
    Of course of this is part of a script then you could user return 1 instead of exit 1
     exit 1
    )
    echo "Exit status is: $?" 
    

提交回复
热议问题