为什么不在导出命令的赋值部分扩展通配符?

Why aren't wildcards expanded in the assignment part of an export command?

我正在编写我自己的 shell(尽可能接近 bash),我正在处理通配符扩展,我看到使用 export 的奇怪行为星号。

bash-3.2$ touch TEST=a
bash-3.2$ touch TEST=b
bash-3.2$ echo TEST=*
TEST=a TEST=b
bash-3.2$ export TEST=*
bash-3.2$ env | grep TEST
TEST=*

似乎星号在某些情况下会扩大,但在调用 export 时不会扩大,这没有多大意义。 bash 中是否有一条我会错过的规则可以解释这种行为?

export 构建的调用采用以下形式:

export [-fn] [-p] [name[=value]]

如果给出了那么它被当作一个参数赋值,这里不会发生文件名扩展,参见参考手册的相关部分:

All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (detailed below). If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((…)) expansion is not used (see Arithmetic Expansion). Word splitting is not performed, with the exception of "$@" as explained below. Filename expansion is not performed.

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html

export 是一个声明实用程序。它的那些类似于变量赋值的参数以与变量赋值相同的方式扩展,即既不对它们执行路径名扩展,也不对它们执行分词,并且值部分受到波浪线扩展。虽然 a bug report was made in 2010, even the most recent edition of the standard fails to document this behavior. However, the changes suggested here were applied in the 202x.1 draft (see Austin Group homepage if you want to obtain a copy), so it is very likely that when the next edition of the standard is published, under Simple Commands,但在第二步的第一句话之后,您会看到下面的声明,该声明强制执行您认为 奇怪.

的行为

If the command name is recognized as a declaration utility, then any remaining words that would be recognized as a variable assignment in isolation shall be expanded as a variable assignment.