[-- Attachment #1: Type: text/plain, Size: 334 bytes --] I'm trying to get the suspected capabilities out of global aliases. Currently I'm using them for: - macro-like labels for exporting patterns like EMPTYSTR being [[:space:][:INVALID:]]# roughly, to a single place, - macro-like SNIP_EMULATE_OPTIONS_ZERO that invokes emulate, setopt and 0=... I wonder what else can global aliases do? [-- Attachment #2: Type: text/html, Size: 589 bytes --]
On Fri, Feb 17, 2023 at 3:59 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I'm trying to get the suspected capabilities out of global aliases. Currently I'm using them for:
> - macro-like labels for exporting patterns like EMPTYSTR being [[:space:][:INVALID:]]# roughly, to a single place,
> - macro-like SNIP_EMULATE_OPTIONS_ZERO that invokes emulate, setopt and 0=...
>
> I wonder what else can global aliases do?
Global aliases are unhygienic macros. It's rarely a good idea to use
unhygienic macros in the code you share with others. If you must use
an unhygienic macro in shared code, give it a long and ugly name to
avoid clashes.
EMPTYSTR can be a parameter. You would have to use $~EMPTYSTR but
you'll avoid most of the pitfalls of unhygienic macros.
SNIP_EMULATE_OPTIONS_ZERO can also be a parameter, which you can use
via `eval $SNIP_EMULATE_OPTIONS_ZERO`. I do something like this in
zsh4humans:
typeset -gr _z4h_opt='emulate -L zsh &&
setopt typeset_silent pipe_fail extended_glob &&
setopt prompt_percent no_prompt_subst no_prompt_bang &&
setopt no_bg_nice no_aliases || return'
function foo() {
eval "$_z4h_opt"
# do stuff
}
function bar() {
eval "$_z4h_opt"
# do stuff
}
Roman.
On Fri, Feb 17, 2023 at 6:59 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I wonder what else can global aliases do?
As Roman has already indicated, it's not a recommended practice to
rely on aliases of any kind in scripts. Aliasing is intended as an
interactive feature to reduce time spent typing commonly-used idioms.
That said, the most common use I've seen made of global aliases is
this kind of thing:
alias -g L='|less'
% ls -lLR L
That is, append L instead of "|less" to commands that are expected to
produce many pages of output.
Similarly stuff like
alias -g TODAYS_FILES='-lrt | fgrep "$(date +"%b %2d")" | awk '\''{print $NF}'\'
% ls TODAYS_FILES
(yes, there's a more zsh-ish way to do that, and it could also be a
command alias by sticking the "ls" in front, just using the pipeline
as an example).
On Fri, Feb 17, 2023, at 9:54 AM, Sebastian Gniazdowski wrote: > I wonder what else can global aliases do? An esoteric but amusing example: 1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug in pre-SVR3 Bourne shells. 2) This doesn't work properly in pre-4.3 zsh because SH_WORD_SPLIT splits ${1+"$@"} too aggressively. 3) But zsh doesn't have a problem with "$@" in the first place, so Autoconf defines a global alias that dynamically replaces ${1+"$@"} with "$@", undoing the original workaround. https://www.gnu.org/software/autoconf/manual/autoconf-2.71/html_node/Shell-Substitutions.html#index-_0022_0024_0040_0022 -- vq
On Fri, Feb 17, 2023 at 10:13 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> 1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug
> in pre-SVR3 Bourne shells.
Fascinating.
This made me Google SVR3. That's System V, Release 3, right? 1987.
Before the first release of bash and zsh.
Roman.
On Fri, Feb 17, 2023, at 4:19 PM, Roman Perepelitsa wrote: > On Fri, Feb 17, 2023 at 10:13 PM Lawrence Velázquez <larryv@zsh.org> wrote: >> >> 1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug >> in pre-SVR3 Bourne shells. > > Fascinating. > > This made me Google SVR3. That's System V, Release 3, right? 1987. > Before the first release of bash and zsh. That's right! Absolutely ancient, but -- to be fair -- downright modern compared to its ancestors [1]. Sensible "$@" [2]? Functions that don't trash the global positional parameters [3]? Sign me up. [1]: https://www.in-ulm.de/~mascheck/bourne/ [2]: https://www.in-ulm.de/~mascheck/various/bourne_args/ [3]: https://www.in-ulm.de/~mascheck/bourne/function_parameters.html -- vq