Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 891019 - app-alternatives/sh: sh[ksh] should warn that local isn't supported
Summary: app-alternatives/sh: sh[ksh] should warn that local isn't supported
Status: CONFIRMED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-16 02:00 UTC by kfm
Modified: 2023-01-17 20:25 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kfm 2023-01-16 02:00:04 UTC
Currently, app-alternatives/sh[ksh] installs and selects app-shells/ksh, which is an actively maintained instance of ksh93, from https://github.com/ksh93/ksh. It is a great shell but, unlike ksh88 (sometimes referred to as "ksh") and its closest descendants, it does not support the local builtin.

Now, where local is supported at all, its behaviour is undefined by POSIX [1] and, given that the last attempt at standardising it floundered [2], the situation is unlikely to change for the foreseeable future. Unfortunately, that doesn't stop people that pretend to be writing portable sh(1) scripts from ignoring the fact and using it anyway. Worse, they tend to use it while assigning values at the same time. Consider the following.

$ dash -c 'f() { local x=1; printf "<%s>\\n" "$x"; }; f; printf "<%s>\\n" "$x";'
<1>
<>

$ ksh -c 'echo "$KSH_VERSION"'
Version AJM 93u+m/1.0.4 2022-10-22
$ ksh -c 'f() { local x=1; printf "<%s>\\n" "$x"; }; f; printf "<%s>\\n" "$x";'
ksh: f[1]: local: not found
<>
<>

Such are the dangers of thumbing one's nose at the standards. Had the assignment occurred separately then, while ksh93 still have failed to understand the local command, x would, at least, have been assigned its intended value.

This is the appropriate way to do it in ksh93.

$ ksh -c 'function f { typeset x=1; printf "<%s>\\n" "$x"; }; f; printf "<%s>\\n" "$x";'
<1>
<>

Note that the "function" keyword must be used. Of course, none of that is portable either.

In summary, ksh93 is a fine shell and would probably serve well enough as a /bin/sh, were it not for the cack-handed use of the non-standard local builtin that is commonplace. Expect supposedly portable scripts not to behave as intended, including many that originate from the Gentoo camp. I would suggest that the ebuild print a warning such as the one below.

"ksh93 does not support the local builtin. Despite its behaviour being undefined by POSIX, many shell scripts make use of it. Such scripts can be expected to malfunction as a consequence of ksh93 being chosen as the sh(1) implementation."

[1] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
[2] https://www.austingroupbugs.net/bug_view_page.php?bug_id=767
Comment 1 kfm 2023-01-16 04:13:13 UTC
Perhaps even "does not support local as a builtin command" for those that aren't quite up to speed with the jargon.