I wish I had had the time to respond to the alias comment thread
earlier. Now, I'd like to make my point more coherently by starting anew
instead.

Let me start with a simple real-life example. In the regression test
suite of my shell library under development, modernish, until recently I
had numbered functions in each test script, doTest1(), doTest2(), etc.
This was a hassle. Inserting or removing test functions required awkward
renumbering. A simple pair of aliases solved my problem:

alias TEST='{ testFn() {'
alias ENDT='}; doTest; }'

Now, each regression test function is delimited by TEST ... ENDT instead
of the normal function definition. The ENDT alias calls a handler
function that runs each test function and automatically numbers the
result as the test script is sourced ('.').

I'm frankly astonished that there seems to be a growing consensus to
de-specify this long-standing type of alias usage. Please, reconsider.

The only objections I can see are of a dogmatic nature. There seems to
be a taboo against "redefining shell syntax", similar to the taboo on
using 'eval', even in safe ways. There is no real technical reason that
it's a "BAD IDEA". The above is perfectly handy, productive and robust.

It is also a highly established and portable usage: it works down to the
first Bourne-derived shell that ever implemented aliases -- because
defining new shell blocks was an original use case proposed for aliases
by David Korn for ksh86. See Korn's 1987 memo at:

- roff source:
https://github.com/weiss/original-bsd/blob/master/local/toolchest/ksh/sh.memo
- PDF: https://www.inlv.org/shellstuff/sh.memo.pdf -- note the date on
it is wrong: it's the date I generated it from the roff source. This is
from 1987.

As Korn wrote there: "The combination of aliases and functions can be
used to do things that can’t be done with either of these separately."

Indeed, it's not as if aliases are some sort of strange anomaly in the
programming world. In other languages, similar features are usually
called 'macros'. C library sources are loaded with them. Nobody calls
that a bad idea, or tells library programmers that they should use
functions instead -- because macros/aliases and functions are good at
different things.

Creative use of aliases may be a Bad Idea for casual shell scripters,
and maybe even many advanced ones -- but only in the sort of way that
using 'eval' is a Bad Idea. It should be done by people who know what
they're doing, who are properly familiar with the shell language and its
internals, in a way that can be re-used by others so they don't have to
reinvent the wheel badly. Hence my belief there is a need for a good
library. But in the long run this won't be possible to do portably if
this functionality is removed or de-sanctioned in the standard.

Please, let's not throw out the baby with the bathwater. Fix the spec if
necessary, but keep aliases working as they are.

- Martijn
  https://github.com/modernish/modernish

Reply via email to