Elizandro - SparcBR
Elizandro - SparcBR

Reputation: 359

Where function is defined when alias of same name exists

I have a function

sql() { ... }

and an alias of same name

alias sql="noglob sql"

I know I can use type somefunction to find the definition of a function, but in this case it doesn't work. The only way I found is to unalias the alias, which is not very practical.

❯ type sql

sql is an alias for noglob sql

❯ unalias sql ; type sql

sql is a shell function from /home/sparc/bin/zsh_libs/zsh_sql

Upvotes: 1

Views: 275

Answers (3)

chepner
chepner

Reputation: 531345

Simply escape part of the command name:

% \sql

Alias expansion happens before quote removal, so the \ prevents a match against an alias from succeeding. A function name lookup happens after quote removal.

% foo () { echo function ;}
% alias foo='echo alias'
% foo
alias
% \foo
function

Upvotes: 2

Elizandro - SparcBR
Elizandro - SparcBR

Reputation: 359

Found a solution

❯ echo $functions_source[sql] 
/home/sparc/bin/zsh_libs/zsh_sql

This won't work on all zsh versions. Check if it exists before using it with:

(($+functions_source)) && echo works

Upvotes: 1

Simba
Simba

Reputation: 27608

# only check function definition by option `-f`
type -f sql

-f
Causes the contents of a shell function to be displayed, which would otherwise not happen unless the -c flag were used.

The solution is ZSH exclusive. You may need to use another option on other shells.

Upvotes: 0

Related Questions