Bug 162007 - ksh-93 doesn't support + command line options
ksh-93 doesn't support + command line options
Product: Fedora
Classification: Fedora
Component: ksh (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Karsten Hopp
Depends On:
  Show dependency treegraph
Reported: 2005-06-28 21:45 EDT by Ian Mortimer
Modified: 2007-11-30 17:11 EST (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-06-30 09:30:11 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Ian Mortimer 2005-06-28 21:45:46 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Fedora/1.0.4-1.3.1 Firefox/1.0.4

Description of problem:
The man page for ksh says that + command line options are supported by the builtin

getopts [  -a name ] optstring vname [ arg ... ]
              parameters are used.  An option argument begins with a + or a -.
              An  option not beginning with + or - or the argument -- ends the
                          ...  The  option letter will be
              prepended with a + when arg begins with a +.

However + options on the command line are not being recognized as options.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. write a simple ksh script with a getopts option loop
2. run it with a + command line option

Actual Results:  The + option is not recognized as an option but is treated as an ordinary argument.

Expected Results:  The + option should have been recognized as an option and prepended with a + as the man page describes.

Additional info:

This simple script shows the problem:

while getopts :n opt
        case $opt in
                n) print "-n selected";;
                +n) print "+n selected";;
shift $(($OPTIND-1))
print "arguments: $*"

When run with pdksh or other ksh implementations with a +n option it produces
the output "+n selected" as expected.  When run with the ksh shipped with FC4
(ksh-93) it produces "arguments: +n" from the final print statement.

The POSIX standard doesn't allow + arguments (it seems) but removing it from ksh
breaks backward compatibility and cross-platform compatibility since it's supported in pdksh and commercial ksh implementations.
Comment 1 Karsten Hopp 2005-06-30 09:30:11 EDT
ksh's man page doesn't cover everything, please use getopts own man page:
ksh> getopts --??

You'll find this section:
If the leading character of optstring is +, then arguments beginning with +
will also be considered options.

A leading : character or a : following a leading + in optstring affects the
way errors are handled. 

If you rewrite your testcase as

while getopts +:n opt
        case $opt in
                n) print "-n selected";;
                +n) print "+n selected";;
shift $(($OPTIND-1))
print "arguments: $*"

everything works as expected.
Comment 2 Ian Mortimer 2005-06-30 18:29:37 EDT
Thanks for the quick solution.

That works and is portable (although not mentioned in the man pages for pdksh
and other ksh implementations).

However it still breaks backward and cross-platform compatibility since the
syntax without the leading + works with other ksh implementations (including pdksh).

Note You need to log in before you can comment on or make changes to this bug.