Curious ksh hack

Brant Cheikes brant at manta.UUCP
Tue Mar 8 13:21:35 AEST 1988


In a memo by David Korn describing ksh, there is a paragraph that
states:

"The ENV file can have an undesirable effect on performance.  Even if
this file is small, the shell must perform an open of this file.  If
large functions are placed in the ENV file they must be read in and
compiled even if they are never referenced.  If you only need the
startup file for interactive shells, then set your ENV variable to a
value which evaluates to a file name for interactive shells and to the
null string otherwise.  If you export the startup file name in the
variable START, then setting

   ENV='${START[(_$-=1)+(_=0)-(_$-!=_{-%%*i*})]}'

will only invoke the startup file for interactive shells since the
subscript evaluates to 0 only if the shell is interactive."

Now, I've done this and it works, but I can't quite figure out how.
I've figured out the following:

  1. $- evaluates to the shell args, for an interactive shell this
is something like 'is', otherwise just 's'.
  2. _$-=1 evaluates to _<shellargs>=1, e.g., _is=1
  3. _=0 is just _=0
  4. _$-!=_{-%%*i*} becomes _is!=_  (if shell args contains 'i') and
     _s!=_s otherwise.

So for an interactive shell, we get
  (_is=1)+(_=0)-(_is!=_)

At this point I'm lost.  Don't the parenthesized items invoke
subshells?  How does this turn out to be 0?  And how does it turn out
to be 1 if the shell is non-interactive?

Can anyone shed some light on how this little hack works?  At least
this little trick should be of general interest, since it's only
documented in the internal ksh memo, not any of the manuals I've been
able to find.
-- 
Brant Cheikes
University of Pennsylvania
Department of Computer and Information Science
ARPA: brant at linc.cis.upenn.edu, UUCP: ...drexel!manta!brant



More information about the Comp.sys.att mailing list