bug-bash
[Top][All Lists]
Advanced

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

Re: Function name conflict with alias in bash-4.3


From: Eric Blake
Subject: Re: Function name conflict with alias in bash-4.3
Date: Tue, 25 Aug 2015 14:39:26 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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