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

Viewed 122

I am working on coding my own shell (As close to bash as possible), I am working on wildcard expansion and I've seen strange behaviour using export with asterisks.

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=*

It seems like the asterisk expands in some cases, but not in the case of a call to export, which doesn't make much sense. Is there a rule in bash that I would have missed which would explain such behaviour?

2 Answers

export is a declaration utility. Those of its arguments that resemble variable assignments are expanded the same way variable assignments are, i.e neither pathname expansion, nor word splitting is performed on them, and the value part is subjected to tilde expansion. Although 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, following the first sentence of step two, you will see the statement below that mandates the behavior you deemed strange.

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.

The call to the export buildin takes the form of:

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

If value is given then it's treated as a parameter assignment, and filename expansion doesn't happen here, see the relevant part of the reference manual:

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

Related