Bug 10936

Summary: Variable assignment with function invocation persists
Product: Busybox Reporter: /df <fieldhouse>
Component: OtherAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Host: Target:
Build:
Attachments: Patch from DASH

Description /df 2018-04-10 12:19:24 UTC
Variable assignments on a command line invoking a shell function persist in the local environment, eg:

BusyBox v1.22.1 (Ubuntu 1:1.22.0-15ubuntu1) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ $ expecting() {
> echo "Expecting ${EXP:-0}"
> }
~ $ expecting
Expecting 0
~ $ EXP=1 expecting
Expecting 1
~ $ expecting
Expecting 1
~ $

This behaviour is to be modified in DASH to match its manual page, which says

"...
The variables which are explicitly placed in the environment of
the command (by placing assignments to them before the function name)
are made local to the function and are set to the values given. ..."

by the patch at https://www.mail-archive.com/dash@vger.kernel.org/msg01579.html, as attached. The undesirable persistent behaviour was apparently a former POSIX requirement.

In case people expect busybox/ash, which I don't think has its own man page, to behave in this way, can a similar change be applied to Busybox?

The affected lines in Busybox are shell/ash.c:10199 for the first change and shell/ash.c:9564-6 for the second.
Comment 1 /df 2018-04-10 21:57:53 UTC
Created attachment 7556 [details]
Patch from DASH
Comment 2 Denys Vlasenko 2018-04-11 10:42:33 UTC
Not in dash git yet.
Comment 3 Denys Vlasenko 2018-08-05 09:16:34 UTC
Fixed in git.