Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable is not exported when assigned in prefix of call to ()-style function #793

Open
sckendall2 opened this issue Nov 10, 2024 · 3 comments
Labels
1.1 Issue relevant to dev branch (v1.1.*) enhancement New feature or request

Comments

@sckendall2
Copy link

To reproduce the bug:

f() { env; }
V=1 f | fgrep -x V=1

In all ksh93 versions I've tried up to and including Version AJM 93u+m/1.0.10 2024-08-01, this snippet prints nothing; the shell doesn't export V into the environment. This behavior contradicts the ksh93 man page:

The environment for any simple-command or function may be augmented by prefixing it with one or more variable assignments... Thus:

TERM=450 cmd args  and
(export TERM; TERM=450; cmd args)

are equivalent...

In other words, the snippet should print V=1. This is what mksh, ksh88, and bash do.

This bug is only with functions defined with the name() syntax. Functions defined with the function name syntax behave correctly.

In fairness, I should note that POSIX section 2.9.1.2 allows either behavior. So it's not implausible to resolve this issue by just documenting the current inconsistent behavior in the man page. But I'd prefer consistency.

@McDutchie McDutchie added enhancement New feature or request 1.1 Issue relevant to dev branch (v1.1.*) labels Nov 14, 2024
@McDutchie
Copy link

We're stuck with the current behaviour for backward compatibility reasons, at least in the 1.0 release series. We can consider changing this for 93u+m/1.1 and later.

@McDutchie
Copy link

Documentation fix for current behaviour:

diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1
index 295ec506f..4280eee59 100644
--- a/src/cmd/ksh93/sh.1
+++ b/src/cmd/ksh93/sh.1
@@ -3999,8 +3999,10 @@ or
 commands.
 .PP
 The environment for any
-.I simple-command\^
-or function
+.IR simple-command\^ ,
+except calls of functions declared with the
+.IB name (\^)
+syntax,
 may be augmented by prefixing it with one or more variable assignments.
 A variable assignment argument is a word of the form
 .IR identifier=value .

@sckendall2
Copy link
Author

I like that you've simply left the effect on the offending functions undefined. But I'll propose a variation on your fix. I think it reads a little more cleanly.

I've also tried to improve an adjacent doc problem: that while the example illustrates the point, the "equivalent" commands

TERM=450 cmd args
(export TERM; TERM=450; cmd args)

are not always equivalent (e.g., if args includes $TERM, or cmd is a function that invokes cd or exit). The qualifier "as far as the above execution of cmd is concerned" works only if you squint at it really hard.

Anyway, here's my variation. I hope I'm not breaking protocol by proposing it here rather than going through git. I can do the latter if you want.

--- sh.1	2024-08-01 18:54:10.000000000 -0400
+++ sh.1a	2024-11-14 16:50:46.590591944 -0500
@@ -3973,7 +3973,10 @@
 .PP
 The environment for any
 .I simple-command\^
-or function
+other than special built-in commands (those listed below that are marked with \fB\(dg\fR),
+or for any function that was not defined with the
+.IB name (\^)
+syntax,
 may be augmented by prefixing it with one or more variable assignments.
 A variable assignment argument is a word of the form
 .IR identifier=value .
@@ -3985,11 +3988,7 @@
 \fB(export \|\s-1TERM\s+1; \|\s-1TERM\s+1=450; \|cmd \|args)\fR
 .RE
 .PP
-are equivalent (as far as the above execution of
-.I cmd\^
-is concerned except for special built-in commands listed below \-
-those that are
-marked with \fB\(dg\fR).
+are roughly equivalent.
 .PP
 If the obsolete
 .B \-k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.1 Issue relevant to dev branch (v1.1.*) enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants