Re: Function name conflict with alias in bash-4.3

2015-08-25 Thread Eric Blake
On 08/25/2015 10:36 AM, Corentin Peuvrel wrote:
> Hi,
> 
> I found a bug in bash-4.3 (4.3.39 in fedora 21), that seems to exists for a 
> long time (at least from 4.1.2 in Centos 6).

Not a bug, but a misunderstanding on your part about how aliases work.

Alias expansion is done on the left-most word PRIOR to anything else;
and if the alias itself expands with a trailing space, then it is also
applied to the next word in turn.  But subsequent words are NOT
considered for alias expansion unless the earlier words followed that rule.

> 
> If you have an alias named "foobar", you can create a function with the 
> keyword "function" (with or without parenthesis) :
> $ function foobar { :; }

Yes, this is using deprecated syntax (these days, it is more portable to
declare functions without using the 'function' keyword).  And because
you don't have:

alias function='function '

defined, alias expansion stops as soon as 'function' is encountered, and
'foobar' cannot be treated as a potential alias needing expansion.

> 
> But you cannot if you don't :
> $ foobar() {:;}
> -bash: syntax error near unexpected token `('

Here, alias expansion sees 'foobar' as a potential word needing
expansion, so it expands it.  And whatever you have it aliased to is
then causing a syntax error when bash then goes to parse the resulting line.

Normally, you can force alias non-expansion with strategic quoting, but
in the case of declaring a function, any quoting you add would render
first word invalid as a function name and therefore also cause problems.

Nothing we can do about it, as it is not a bug, but a documented design
consideration.

> 
> It's not really critical once you know it, but it's a little bit odd  !

And while there are one or two legitimate reasons for aliases to still
exist (there are some effects that HAVE to be done prior to parse time,
and cannot be deferred to a function, such as temporarily disabling
globbing for the duration of a parse), you have just (re-)discovered one
of the reasons why we strongly recommend the use of functions rather
than aliases in the majority of modern code.

-- 
Eric Blake   eblake redhat com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: Function name conflict with alias in bash-4.3

2015-08-25 Thread Greg Wooledge
On Tue, Aug 25, 2015 at 06:36:34PM +0200, Corentin Peuvrel wrote:
> If you have an alias named "foobar", you can create a function with the 
> keyword "function" (with or without parenthesis) :
> $ function foobar { :; }
> 
> But you cannot if you don't :
> $ foobar() {:;}
> -bash: syntax error near unexpected token `('

unalias foobar
foobar() { ...; }



Function name conflict with alias in bash-4.3

2015-08-25 Thread Corentin Peuvrel
Hi,

I found a bug in bash-4.3 (4.3.39 in fedora 21), that seems to exists for a 
long time (at least from 4.1.2 in Centos 6).

If you have an alias named "foobar", you can create a function with the keyword 
"function" (with or without parenthesis) :
$ function foobar { :; }

But you cannot if you don't :
$ foobar() {:;}
-bash: syntax error near unexpected token `('

It's not really critical once you know it, but it's a little bit odd  !

Thank you,

Corentin Peuvrel