# exit

退出当前的shell。

## 概要

```
exit [n]
```

## 主要用途

* 执行exit可使shell以指定的状态值退出。若不设置参数，则以最后一条命令的返回值作为exit的返回值退出。

## 参数

n（可选）：指定的shell返回值（整数）。

## 返回值

返回值为你指定的参数n的值，如果你指定的参数大于255或小于0，那么会通过加或减256的方式使得返回值总是处于0到255之间。

## 例子

退出当前shell：

```
[root@localhost ~]# exit
logout
```

也可以使用`ctrl+d`退出当前终端，下面列出了打开或关闭该功能的方法：

```
# 打开ctrl+d退出终端
set -o ignoreeof
# 关闭ctrl+d退出终端
set +o ignoreeof
```

在脚本中，进入脚本所在目录，否则退出：

```
cd $(dirname $0) || exit 1
```

在脚本中，判断参数数量，不匹配就打印使用方式，退出：

```
if [ "$#" -ne "2" ]; then
    echo "usage: $0 <area> <hours>"
    exit 2
fi
```

在脚本中，退出时删除临时文件：

```
trap "rm -f tmpfile; echo Bye." EXIT
```

检查上一命令的退出码：

```
./mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
    echo "O.K"
fi
```

### 注意

1. 该命令是bash内建命令，相关的帮助信息请查看`help`命令。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/linux/ming-ling/exit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
