bug-bash
[Top][All Lists]
Advanced

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

Re: Having an alias and a function with the same name leads to some sort


From: Robert Elz
Subject: Re: Having an alias and a function with the same name leads to some sort of recursion
Date: Sat, 18 Feb 2023 12:03:34 +0700

    Date:        Sat, 18 Feb 2023 12:21:49 +1000
    From:        Martin D Kealey <martin@kurahaupo.gen.nz>
    Message-ID:  
<CAN_U6MVFHhMzQzsSc9-Xh_cyg+Njxr-Jy8s5=myW=kdJmHNVPQ@mail.gmail.com>

  | Both of these are clearly wrong,

Yes, which is why POSIX is changing.  I am concerned less about
bash (and other shell) doc about the details of all of this,
more useful to suggest that users don't use aliases at all than
to precisely explain everything about how they work.

So:
  | I want to find a way of expressing all this in English without
  | assuming the reader knows what "look ahead" and "state"
  | mean in compiler theory.

I wouldn't bother.  Chet's suggested text, without the
bracketed inserts, looks fine to me.

  | I feel that "completely lexical" is misleading.

Depends how the shell is to operate.   In most shells
if the input is (currently in a state where a command
word might appear)

        cmd&

the '&' delimits the word token 'cmd', we are in a place
aliases can occur, and 'cmd' is not a reserved word.

If we had earlier processed

        alias cmd='foo>'

then alias substitution results in

        foo>&

in which (assuming foo is not another alias) we have
2 lexical tokens, the word (TOKEN in posix speak) "foo"
and the redirection operator ">&".

Shells are not required by POSIX to do that, but a lot of
users seem to like aliases to do that kind of thing.

If you do:
  | aliases could equally well be implemented br splitting the
  | substitution string into lexical tokens when the alias is
  | defined, and then substituting that sequence of tokens when
  | the alias is expanded

you will necessarily result in 3 tokens above.   This
example is not completely absurd (more than is necessarily
true because aliases are involved) and can be useful,
the user might often want to run 'foo' with stdout
redirected, and so define the cmd alias, with normal
usage being

        cmd file

resulting in

        foo> file

but sometimes the user might want to append, avoid -C
being set when file already exists, or output to a fd

        cmd>file  cmd|file   cmd&3

accomplish that in a shell which processes alias completely
lexically.   Not in others.

And no, I do not recommend any of this, or use of aliases
at all, but some users seem to like this nonsense.

kre

ps: note that "foo" above might in actual use be:
        /absurdly/long/pathname/to/a/command/not/found/in/PATH/foo
so just suggesting to the user that they use "foo>>" instead
of "cmd>" (etc) doesn't always meet with a positive response.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]