# Shell函数

shell脚本可以定义函数，供其他脚本调。

### 1. 函数的定义

shell脚本使用`function`关键字和 `{}` 来定义函数。\
​

```shell
function func1() {

echo "this is func1"

}
```

* 函数的定义可以使用`function`关键字，也可以不用
* 函数不会显式的定义参数，只用一个占位用的 `()`
* 函数体定义在`{}`之间
* 函数的返回值可以通过 `return` 显式返回，如果不使用 `return` , 将使用最后一条命令的运行结果作为返回值， 返回的范围在\[0,255]

### 2. 函数的参数

在函数体中，使用 `$n` 这样的模式访问参数\
​

​

### 3. 调用函数

调用函数就像执行一条命令一样\
​

```shell
# 调用函数func1
func1 

# 调用函数并传递参数 
func1 1 2 2 3
```

​

​


---

# 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://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/shell/shell-han-shu.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.
