bug-bash
[Top][All Lists]
Advanced

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

Re: unalias works weirdly inside an if-then block


From: Greg Wooledge
Subject: Re: unalias works weirdly inside an if-then block
Date: Fri, 12 May 2017 09:10:07 -0400
User-agent: Mutt/1.4.2.3i

On Fri, May 12, 2017 at 02:33:06PM +0200, Gabor Burjan wrote:
>       unalias works weirdly inside an if-then block

Because bash has to parse the entire compound command (the entire
multi-line command up to "fi") before it can begin execution of it.

> alias cp='cp -i'
> 
> if [[ 1 = 1 ]]; then
>     unalias cp
>     cp /tmp/a /tmp/b
> fi

Bash reads the entire if...fi command, parses it (which includes expansion
of the alias), and then begins executing it.  The cp command has already
been replaced by cp -i.

> # This one won't be interactive
> cp /tmp/a /tmp/b

This command isn't read/parsed until after the "unalias" has already
executed.

There is a similar issue with "shopt -s extglob".  Attempting to use it
to change the parser while inside a compound command (e.g. a function)
does not work the way people expect.

Commands that change the parser (alias, unalias, shopt -s extglob) must
be executed at the top of the script, outside of any functions, and
should be in effect for the entire script.  Attempting to toggle the
parser between states on the fly is a really bad idea.



reply via email to

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