[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Possible Bug in BASH
From: |
Bob Proulx |
Subject: |
Re: Possible Bug in BASH |
Date: |
Sat, 20 Jun 2009 16:47:09 -0600 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
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.
> 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.
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.
Bob