在Linux中,exit
命令用于退出当前运行的程序或脚本
在shell脚本中使用exit
命令:
在shell脚本中,你可以使用exit
命令来退出脚本。当脚本执行到exit
命令时,脚本会立即终止,后面的命令不会被执行。例如:
#!/bin/bash
echo "This is a sample script."
exit 0
echo "This line will not be executed."
在这个例子中,脚本会在打印"This is a sample script."后立即退出,因此"This line will not be executed."不会被执行。
在命令行中使用exit
命令:
在命令行中,你可以使用exit
命令来退出当前shell会话。当你在命令行中执行exit
命令时,当前shell会话会立即终止。例如:
$ echo "This is a sample command."
This is a sample command.
$ exit
在这个例子中,命令行会话会在打印"This is a sample command."后立即退出。
注意:exit
命令通常以0作为参数,表示程序或脚本已成功执行。你可以使用非零值表示程序或脚本遇到了错误。例如:
#!/bin/bash
echo "This is a sample script."
exit 1
echo "This line will not be executed."
在这个例子中,脚本会打印"This is a sample script.",然后使用exit 1
命令退出,表示脚本遇到了错误。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Linux通配符如何用于脚本编程