Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

前端 未结 5 764
悲&欢浪女
悲&欢浪女 2020-11-29 18:19

I was using \"exit 1\" statement in my bash functions to terminate the whole script and it worked fine:

function func()
{
   echo \"Goodbye\"
   exit 1
}
ech         


        
5条回答
  •  -上瘾入骨i
    2020-11-29 18:45

    You can use set -e which exits if a command exits with a non-zero status:

    set -e 
    func
    set +e
    

    Or grab the return value:

    (func) || exit $?
    

提交回复
热议问题