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.