Search code examples
zsh

zsh temporarily disable alias within a function


I feel that zsh will expand aliases even it is called within a function, for example

alias abc='echo abc'
function fabc(){abc}

Is it possible to disable alias expansion in this function?

One more related question: Is it possible to disable alias expansion in the whole interactive shell?


Solution

  • You can disable a particular alias with

    unalias abc
    

    or all aliases with

    unalias -a
    

    Another solution is to force a command not to use an alias with a backslash

    \abc
    

    The problem is more difficult when used in a function... It seems from here, you cannot define or undefine your aliases in a function.

    Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command.

    So you may want to do something like

    alias abc='echo abc'
    
    myaliases=$(alias -L)
    unalias -a
    function fabc(){
        abc
    }
    eval $myaliases; unset myaliases
    
    fabc
    abc