You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
getopts: ---foo or +++foo not treated as options #1053
It rather looks -s are ignored to some extent as long as there aren't more than one in sequence.
[f:foobar] for instance alllows --foo-bar but not --foo--bar nor ---foobar (treated as non-option which is my main issue here), while [f:foo-bar] allows --foobar, --foo-bar and --foo--bar.
Yes, I agree ignoring -s looks like a feature (even if undocumented AFAICT like for the no/NO prefix) and there appears to be some support (intentional or accidental I don't know) to support both --foobar and --foo-bar if that's the user 's intention, but to me ---foobar being treated as non-options is a bug. The only thing starting with - that should not be treated as an option should be the -- option delimiter (and discarded).
Some of the findings I mention there may be worth adding to the documentation.
Shells such as bash, zsh or fish have information in yold / texinfo / (not sure for fish) format, which results in many pages (info / html / man) on the backends. ksh93 documentation was split into several pages as being part of ast-open, and there are terse builtin --man pages for most builtins. Is there a plan to consolidate all that into some more global documentation that one could refer to?
Is there a plan to consolidate all that into some more global documentation that one could refer to?
Some others already had a similar thought. See #902. That project seems to have gone quiet though.
My plate is more than full with fixing endless bugs. I'm not even getting to the new feature development I've wanted to do for years, and quite frankly, I'm worn out.
Any other work people want done, they'll have to do themselves, and submit a PR.
One may/might want to use that in ksh scripts, if only as wrappers to those other commands that handle ---options (though that seems very unlikely).
I don't think POSIX has any bearing on this. I'd expect it would be all unspecified behaviour there. As far as the getopts spec is concerned, "The use of other option characters that are not alphanumeric produces unspecified results." would cover it. As for any some-builtin-specified-by-POSIX ---qwe not returning an error, again I'd expect it would be unspecified.
If I had to venture a guess, I'd say maybe that change was introduced so as not to have to try and reconciliate the -s somewhat ignored in long option names and allowing users to specify options with - in their name. You might find that reverting that change introduces all sorts of different bugs.
I can't imagine why anyone would want, need or rely on that behaviour. It is quite surprising.
Agreed.
If I had to venture a guess, I'd say maybe that change was introduced so as not to have to try and reconciliate the -s somewhat ignored in long option names and allowing users to specify options with - in their name. You might find that reverting that change introduces all sorts of different bugs.
Or then again it might be just fine, because the AST/ksh folks have a long and distinguished history of committing intentional dysfunctionality in their code for no discernible reason. My list of bugs fixed purely by removing code now has 41 items on it and this might be the 42nd. Only one way to find out... remove it and see what happens.
After fixing this, there could be incompatibilities if a script does 'set $foo' with foo containing a first field that starts with at least three dashes.
So, to be on the safe side, this should probably be fixed on the dev branch only.
I don't think POSIX has any bearing on this. I'd expect it would be all unspecified behaviour there. As far as the getopts spec is concerned, "The use of other option characters that are not alphanumeric produces unspecified results." would cover it.
Maybe. Another way to look at it is that, POSIXly, either a -- argument or any argument not starting with - terminates options and everything else is an operand, and the change in question violates that.
Maybe. Another way to look at it is that, POSIXly, either a -- argument or any argument not starting with - terminates options and everything else is an operand, and the change in question violates that.
Ah yes, sorry, you're right. I was only considering getopts users wanting to specify a - option but of course POSIX requires:
getopts :o opt ---foo; echo "$? $opt $OPTARG"
For instance to output 0 ? -, whilst ksh outputs 1 ?.
$ ksh --posix -c 'getopts o o -é'
ksh: -�: unknown option
$ ksh --posix -c 'getopts é o -é; echo "$? $o"'
0 �
(where � is my terminal replacing the first byte of the encoding of é).
But then again, all shells I tried (and all utilities using the getopt() / getopt_long() APIs at least on my Debian system) except yash have the same problem. Likely an issue to clarify at POSIX first given that how https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getopt.html would cope with multibyte characters is not very clear.
Activity
stephane-chazelas commentedon Oct 11, 2026
Also note:
As if
[f:-foo]was treated as[f:foo]though there's still a difference in:stephane-chazelas commentedon Oct 11, 2026
It rather looks
-s are ignored to some extent as long as there aren't more than one in sequence.[f:foobar]for instance alllows--foo-barbut not--foo--barnor---foobar(treated as non-option which is my main issue here), while[f:foo-bar]allows--foobar,--foo-barand--foo--bar.McDutchie commentedon Oct 12, 2026
That is a feature, not a bug. It's similar with ksh long-form shell options (although they consistently ignore all dashes except in the
noprefix).stephane-chazelas commentedon Oct 12, 2026
Yes, I agree ignoring
-s looks like a feature (even if undocumented AFAICT like for theno/NOprefix) and there appears to be some support (intentional or accidental I don't know) to support both--foobarand--foo-barif that's the user 's intention, but to me---foobarbeing treated as non-options is a bug. The only thing starting with-that should not be treated as an option should be the--option delimiter (and discarded).stephane-chazelas commentedon Oct 12, 2026
FYI, the few getopts-related issues I've raised lately were as a result of trying to answer https://unix.stackexchange.com/questions/482119/does-ksh93-support-o-type-options-with-its-getopts1-builtin
Some of the findings I mention there may be worth adding to the documentation.
Shells such as bash, zsh or fish have information in yold / texinfo / (not sure for fish) format, which results in many pages (info / html / man) on the backends. ksh93 documentation was split into several pages as being part of ast-open, and there are terse
builtin --manpages for most builtins. Is there a plan to consolidate all that into some more global documentation that one could refer to?McDutchie commentedon Oct 13, 2026
Some others already had a similar thought. See #902. That project seems to have gone quiet though.
My plate is more than full with fixing endless bugs. I'm not even getting to the new feature development I've wanted to do for years, and quite frankly, I'm worn out.
Any other work people want done, they'll have to do themselves, and submit a PR.
McDutchie commentedon Oct 13, 2026
Looks like the reported behaviour was intentional. In
src/lib/libast/RELEASE:McDutchie commentedon Oct 13, 2026
The offending code:
ksh/src/lib/libast/misc/optget.c
Lines 4497 to 4504 in 4959900
So, since it was intentional, there are three options:
stephane-chazelas commentedon Oct 13, 2026
Good find.
I can't imagine why anyone would want, need or rely on that behaviour. It is quite surprising.
Having
---foobarfor undocumented / hidden options is somewhat common. See for instance https://unix.stackexchange.com/questions/778721/convention-of-parameter-naming-undocumented-options/One may/might want to use that in ksh scripts, if only as wrappers to those other commands that handle
---options(though that seems very unlikely).I don't think POSIX has any bearing on this. I'd expect it would be all unspecified behaviour there. As far as the
getoptsspec is concerned, "The use of other option characters that are not alphanumeric produces unspecified results." would cover it. As for anysome-builtin-specified-by-POSIX ---qwenot returning an error, again I'd expect it would be unspecified.If I had to venture a guess, I'd say maybe that change was introduced so as not to have to try and reconciliate the
-s somewhat ignored in long option names and allowing users to specify options with-in their name. You might find that reverting that change introduces all sorts of different bugs.McDutchie commentedon Oct 13, 2026
Agreed.
Or then again it might be just fine, because the AST/ksh folks have a long and distinguished history of committing intentional dysfunctionality in their code for no discernible reason. My list of bugs fixed purely by removing code now has 41 items on it and this might be the 42nd. Only one way to find out... remove it and see what happens.
McDutchie commentedon Oct 13, 2026
FWIW, after removing it, there are 0 regression test failures (so they didn't even add a regression test for this).
McDutchie commentedon Oct 13, 2026
After fixing this, there could be incompatibilities if a script does 'set $foo' with foo containing a first field that starts with at least three dashes.
So, to be on the safe side, this should probably be fixed on the dev branch only.
McDutchie commentedon Oct 13, 2026
On the other hand... my policy item 4 says: "No 100% bug compatibility. Broken and undocumented behaviour gets fixed."
They listed the change, extremely tersely, in the RELEASE file, and nowhere else. Does that count as "documented"? It's iffy.
McDutchie commentedon Oct 13, 2026
By the way, I found an undocumented feature: terse columnar format for the current shell options state.
Implemented in args.c, lines 153-160, 314-315, and 572-618.
After removing those getopts.c lines that feature also works via
set ---.McDutchie commentedon Oct 13, 2026
Maybe. Another way to look at it is that, POSIXly, either a
--argument or any argument not starting with-terminates options and everything else is an operand, and the change in question violates that.stephane-chazelas commentedon Oct 14, 2026
Ah yes, sorry, you're right. I was only considering
getoptsusers wanting to specify a-option but of course POSIX requires:For instance to output
0 ? -, whilst ksh outputs1 ?.But then again, that also applies to:
Even though in that case, that
--helpautomated handling is actually useful even if breaking POSIX compliance.BTW:
I suppose that's because
--usageis short for--??usage.I see some changes in that regards on current
devversion:While this is still a problem:
stephane-chazelas commentedon Oct 14, 2026
Also:
(where � is my terminal replacing the first byte of the encoding of
é).But then again, all shells I tried (and all utilities using the
getopt()/getopt_long()APIs at least on my Debian system) exceptyashhave the same problem. Likely an issue to clarify at POSIX first given that how https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getopt.html would cope with multibyte characters is not very clear.