Possible Bug in BASH
I think I may have found a bug in BASH 3.2.17(1)-release on a mac. Below is a simple demonstration of the unexpected behavior: SHELL 1: mattsh$ alias c=clear mattsh$ c () { echo foo; } mattsh$ clear foo SHELL 2: mattsh$ alias c=clear mattsh$ function c () { echo foo; } mattsh$ clear [blanks screen] The documentation for 'function' states: function: function NAME { COMMANDS ; } or NAME () { COMMANDS ; } Create a simple command invoked by NAME which runs COMMANDS. Arguments on the command line along with NAME are passed to the function as $0 .. $n. According to this, "function NAME { COMMANDS ; }" and "NAME () { COMMANDS ; }" should be equivalent. If a change is made to either the documentation or code, I would appreciate a mention, although this is not required. Regardless, please let me know either way. -- ~Matthew Strax-Haber Co-op, NASA, Langley Research Center Undergrad, Northeastern University
Re: Possible Bug in BASH
On Jun 20, 2009, at 3:47 PM, Bob Proulx wrote: > Matthew Strax-Haber wrote: >> I think I may have found a bug in BASH 3.2.17(1)-release on a mac. > > I am not sure it is a bug but it does seem ood. Perhaps it is not a bug, but if it is not then the documentation is inconsistent with the behavior. > >> Below is a simple demonstration of the unexpected behavior: >> >> SHELL 1: >> mattsh$ alias c=clear >> mattsh$ c () { echo foo; } >> mattsh$ clear >> foo > > Here c is the first word. So it is replaced by the alias. (I didn't > know this behavior before. This is actually really surprising > behavior to me since I didn't expect a function definition to be a > simple command.) > >> SHELL 2: >> mattsh$ alias c=clear >> mattsh$ function c () { echo foo; } >> mattsh$ clear >> [blanks screen] > > Here function is the first word. > > Useful in this context is 'type -a c' and type -a clear' to view the > list of possible definitions of c and clear. > >> The documentation for 'function' states: >> function: function NAME { COMMANDS ; } or NAME () { COMMANDS ; } >>Create a simple command invoked by NAME which runs COMMANDS. >>Arguments on the command line along with NAME are passed to the >>function as $0 .. $n. >> >> According to this, "function NAME { COMMANDS ; }" and "NAME () { COMMANDS >> ; }" should be equivalent. > > Yes. But the documentation for alias says: > >Aliases allow a string to be substituted for a word when it is >used as the first word of a simple command. > > And the definition of reserved words says: > >Reserved words are words that have a special meaning to the >shell. The following words are recognized as reserved when >unquoted and either the first word of a simple command (see >SHELL GRAMMAR below) or the third word of a case or for >command: > >! case do done elif else esac fi for function if in select then >until while { } time [[ ]] > > Therefore function is a reserved word when it is used as the first > word of a simple command. Meaning to me that the name c can't be the > first word in that case because function is the first word. Meaning > that c isn't replaced by the alias in that case. It is only replaced > when it is the first word. I understand what is happening and why, but from my understanding either the implementation should be changed or the documentation made more precise. > Using aliases in this way seems really warped to me. I would avoid it > for the clarity of code. Aliases are really an old paradigm from csh > days. Functions in more modern shells completely replace the need for > aliases. Agreed. > Bob -- ~ Matthew Strax-Haber Northeastern University