Shell 文件包含
核对 Shell 使用点号(.)与 source 在当前进程上下文中引入外部公共脚本的语法、权限要求及环境变量共享行为。
学完这一章,你能够
- 核对 Shell 使用点号(.)与 source 在当前进程上下文中引入外部公共脚本的语法、权限要求及环境变量共享行为。
文件包含语法:点号(.)与 source
为了复用公共变量、配置与工具函数,Shell 支持在当前脚本中直接包含外部脚本文件,提供两种语法:POSIX 标准的点号写法 `. filename`(注意 `.` 与文件名之间必须有空格)以及 Bash/Zsh 等提供的可读别名 `source filename`。两者都在当前 Shell 进程内逐行读取并执行目标脚本,而非派生子进程。
跨脚本引入变量与权限边界实测
典型用法是将公共配置或函数库写入 `test1.sh`,在主脚本 `test2.sh` 中通过 `. ./test1.sh` 或 `source ./test1.sh` 引入,再为入口脚本 `test2.sh` 添加可执行权限运行。重要细节在于:被包含文件 `test1.sh` 仅需可读权限,不需要可执行(`+x`)权限。本站离线容器实测将 `test1.sh` 设为 `0644`(确认 `[ -x ]` 为假)、`test2.sh` 设为 `0755` 并通过 `. ./test1.sh` 引入,成功读取其变量 `url` 与函数 `greet_lib`;另实测 `source` 引入 `env_mod.sh`,证实前后 `$$` 进程 ID 完全一致且被包含脚本对 `SHARED_STATE` 的修改直接保留在调用方环境中。
运行 GNU bash 5.2 点号(.)无执行位包含与 source 同进程环境共享实测
展开查看收起
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C.UTF-8
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT
VERSION="Debian bookworm-slim GNU bash 5.2.15 (--network none)"
CASES=()
json_str() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\n'/\\n}
s=${s//$'\r'/\\r}
s=${s//$'\t'/\\t}
printf '"%s"' "$s"
}
add_case() {
local name="$1" summary="$2" expected="$3" actual="$4"
local passed="false"
if [[ "$expected" == "$actual" ]]; then
passed="true"
fi
CASES+=("{\"name\":$(json_str "$name"),\"summary\":$(json_str "$summary"),\"expected\":$(json_str "$expected"),\"actual\":$(json_str "$actual"),\"passed\":$passed}")
}
emit_json() {
printf '{"version":%s,"cases":[' "$(json_str "$VERSION")"
local i
for ((i = 0; i < ${#CASES[@]}; i++)); do
if ((i > 0)); then printf ','; fi
printf '%s' "${CASES[$i]}"
done
printf ']}\n'
}
# Case 1: dot_include_without_exec_bit - . ./test1.sh works when test1.sh is 0644 (non-executable)
cat <<'EOF' > "$WORKDIR/test1.sh"
#!/bin/bash
url="https://docs.example.org"
greet_lib() {
echo "lib:$url"
}
EOF
chmod 0644 "$WORKDIR/test1.sh"
cat <<'EOF' > "$WORKDIR/test2.sh"
#!/bin/bash
. ./test1.sh
echo "url=$url;fn=$(greet_lib)"
EOF
chmod 0755 "$WORKDIR/test2.sh"
is_t1_exec=$([ -x "$WORKDIR/test1.sh" ] && echo 1 || echo 0)
out_dot=$(cd "$WORKDIR" && ./test2.sh)
add_case \
"dot_include_without_exec_bit" \
"验证被包含脚本 test1.sh 无需可执行权限(0644),主脚本通过 . ./test1.sh 可读取其变量与函数" \
"t1_exec=0;out=url=https://docs.example.org;fn=lib:https://docs.example.org" \
"t1_exec=${is_t1_exec};out=${out_dot}"
# Case 2: source_include_same_shell_env - source executes in current shell process (same PID, mutates caller vars)
cat <<'EOF' > "$WORKDIR/env_mod.sh"
INCLUDED_PID="$$"
SHARED_STATE="updated_in_included"
EOF
chmod 0644 "$WORKDIR/env_mod.sh"
SHARED_STATE="initial"
CALLER_PID="$$"
source "$WORKDIR/env_mod.sh"
same_pid=$([ "$CALLER_PID" = "$INCLUDED_PID" ] && echo 1 || echo 0)
add_case \
"source_include_same_shell_env" \
"验证 source 在当前 Shell 进程上下文执行(\$\$ 相同),且被包含文件中的变量变更直接作用于调用方" \
"same_pid=1;state=updated_in_included" \
"same_pid=${same_pid};state=${SHARED_STATE}"
emit_json
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
./helper.sh 会启动一个独立的子 Shell 进程,子进程中定义的变量与目录切换在结束后不会回传给父 Shell;而 source(或 .)在当前 Shell 进程内直接求值,所有变量、函数与环境变更都会保留在调用方中,且被包含文件不需要可执行权限。