Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh temporarily disable alias within a function

Tags:

zsh

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?

like image 898
doraemon Avatar asked Oct 21 '25 12:10

doraemon


1 Answers

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
like image 55
rools Avatar answered Oct 23 '25 08:10

rools