shell脚本中exit命令

2020-09-30 10:19:54

exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。

$ exit --help
exit: exit [n]
    Exit the shell.

    Exits the shell with a status of N.  If N is omitted, the exit status
    is that of the last command executed.

常用方法:

格式:exit n
退出shell脚本,并设置退出码为n。

格式:exit
退出shell脚本,退出码为最后一个命令的退出码(即 $?)。

格式:trap “commands” EXIT
退出前执行commands指定的命令。

常用退出码

0表示成功(Zero - Success)

非0表示失败(Non-Zero - Failure)

2表示用法不当(Incorrect Usage)

127表示命令没有找到(Command Not Found)

126表示不是可执行的(Not an executable)

128 信号产生

posted @ 2022-06-17 09:47  QIYUEXIN  阅读(770)  评论(0编辑  收藏  举报