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