# source

在当前Shell环境中从指定文件读取和执行命令。

## 概要

source filename \[arguments]

## 主要用途

* 执行文件并从文件中加载变量及函数到执行环境

### 参数

filename：要执行的文件

arguments（可选）：传递给文件的参数

### 返回值

source返回文件最后一个命令的返回值，如果文件不能读取则会失败

### 错误用法

* 文件在`$PATH`中找不到。
* 文件未给出。

## 例子

* 在一些工具的执行过程中，会把环境变量设置以"export XXX=XXXXXX"或"declare XXX=XXXXXX"的形式导出到 一个文件中，然后用source加载该文件内容到执行环境中。
* 读取和执行/root/.bash\_profile文件。

```
[root@localhost ~]# source ~/.bash_profile
```

## Q\&A

Q：`source`和`sh`在执行文件方面有什么区别？

A：`sh`的执行是在子shell中，`source`会使得被执行文件的变量及函数加载进当前终端环境内（除去函数内local修饰的变量等）；建议您参考`export`命令的 **知识点** 部分

## 注意

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/source.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.
