Alliot's blog

shell中exit 0与exit 1

  shell 脚本中有时候需要运行退出脚本操作,可以使用 exit 来实现退出脚本

exit 0

  使用 eixt 0 退出的脚本,表示脚本正常运行退出,返回 0,运行后 $? 的值为 0
如:

1
2
3
4
5
6
7
8
[root@localhost ~]# cat alliot.sh 
#!/bin/bash
echo "exit 0 test"
exit 0
[root@localhost ~]# ./alliot.sh
exit 0 test
[root@localhost ~]# echo $?
0

exit 1

  使用 eixt 1 退出的脚本,表示脚本非正常运行退出,返回 1,运行后 $? 的值为 1
如:

1
2
3
4
5
6
7
8
[root@localhost ~]# cat alliot.sh 
#!/bin/bash
echo "exit 1 test"
exit 1
[root@localhost ~]# ./alliot.sh
exit 1 test
[root@localhost ~]# echo $?
1

------ 本文结束 ------

本文标题:shell中exit 0与exit 1

文章作者:Alliot

发布时间:2019年07月18日 - 11:07

最后更新:2023年05月21日 - 00:05

原始链接:https://www.iots.vip/post/shell-exit0-exit1.html

许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 转载请保留原文链接及作者。

若文章为您解决燃眉之急或是带来些许明朗,不妨打赏 Alliot 一杯香茗或是一杯咖啡