在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
命令退出,表示腳本遇到了錯誤。