Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/65902280/why-arent-wildcards-expanded-in-the-assignment-part-of-an-export-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...