NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: ksh and brace expansion



    Date:        Tue, 28 Jan 2020 18:29:48 +0000 (UTC)
    From:        adr <adr%SDF.ORG@localhost>
    Message-ID:  <alpine.NEB.2.21.2001281710190.25985%sdf.lonestar.org@localhost>

  | What I mean is do not worry so much to make ksh POSIX compliant by
  | default. In my opinion is better to do that just with sh.

No-one is worried too much about either.   But I think you missed my
point, POSIX is based upon ksh, a correct (ie: bug free) ksh should be
posix compliant, almost just by being ksh.   Sure, it has extensions,
but those are permitted by POSIX, as long as they don't alter the effect
of correctly written (ie: properly conforming) scripts.

Unfortunately brace expansion does.   At least currently - POSIX is
likely to recommend quoting '{' in cases where shells that implement
brace expansion would perform that, so as to at least permit brace
expansion as an exention.

Note that the other issue of actually standardising brace expansion,
is when in the sequence of events it happens.   In csh (from whence
brace expansion originated) it is more or less the first thing that
happens to a command line, before anything else.  This means that
	ls -l ~{me,him,her}/bin
works as you'd hope it would.

In ksh (or at least most of them) brace expansion happens after
tilde, var, arith, and command substitutions (and I think, field
splitting) - just before pathname expansion.   This means that
	ls -l $(echo '{me,him,her}')/bin
works (though I'm not sure why anyone would care) - and that brace
expansions can be embedded into variables (expanded after the var is
expanded).   But the example in the previous paragraph doesn't work.

  | It is quite a shame that rc didn't become popular.

Agreed, it was nice.   But it was too different - by the time rc
became available, there were far too many scripts/Makefiles/... using
Bourne sh syntax for anything that different to have much of a chance.
Inertia usually wins.

  | From sh(1) I thought that the the goal was for sh to be POSIX.

No, as it says:

  | [...]
  | sh is the standard command interpreter for the system.  The current
  | version of sh is in the process of being changed to conform more closely
  | to the POSIX 1003.2 and 1003.2a specifications for the shell.
  | [...]

"conform more closely" is not "be".   And yes, we do that, whenever we
differ from POSIX, and POSIX is rational, we try to make sh act the way
POSIX specifies - that ends up being long term perferable, even if it can
sometimes cause some short term pain.   But when POSIX is ridiculous, as
it is in a few places, we simply ignore it.

  | An example where it seams useful, is when a command takes the same
  | option several times with different arguments:

There are many (interactive) uses where brace expansion is useful
(ie: saves the weary fingers), but putting that in a script, instead
of doing a little cut & paste in your editor, and expanding it, is
unforgivable.

  | I didn't expect brace expansion disable by default.

It isn't.    You must have POSIXLY_CORRECT set if it was.   And in
that case, if you're asking for a POSIX environment, it is reasonable,
I believe, that that is what you're given.   Since brace expansion is
incompatible (currently) with POSIX requirements, it cannot be on by
default in POSIX mode.

If you don't care about being strictly POSIX compatible, don't define
POSIXLY_CORRECT.

  | Reading again the man page (just 2886 lines...), it is clear
  | that posix can be on by default,

It can (or so I believe, building ksh isn't one of my fetishes) but
no-one (and certainly not here) builds it that way.

If you want a Korn sh, you'd probably be better served by shells/mksh
from pkgsrc than our ksh - they started in the same place, long ago, but
mksh gets lots of care and attention, and has far fewer bugs.   But note
that POSIXLY_CORRECT at startup turns off braceexpand in mksh as well.

mksh also turns on POSIX mode (and hence turns off braceexpand) if the
binary that contains it (or more accurately, $0) is "sh*" or "*/sh*"
rather than "*/mksh" or "*/ksh", as POSIX specifies that "sh" found from
the standard $PATH should be a POSIX sh.

kre






Home | Main Index | Thread Index | Old Index