ewok
ewok

Reputation: 21443

bash: force function to call alias

I have a function that I use in place of cd, and I have cd aliased to call it. My question is, is it possible to force all functions and scripts to call my cd_func through the alias, rather than calling cd directly.

Here's an example using ls, because it's simpler:

.myfile:

ls_func(){
    echo "function called"
    dir
}
alias ls=ls_func

.notmyfile

ls_func2(){
    ls
}

It's important to note that .notmyfile is sourced before .myfile is. What I want is to force ls_func2 to call ls_func instead of ls directly.

Upvotes: 0

Views: 127

Answers (1)

Norbert
Norbert

Reputation: 6084

Sourcing in bash overwrites functions which are the same, so the source order in this case does not matter (until you call a function before the related function is loaded.

To make this work add this to the top of your script:

shopt -s expand_aliases

This command is used to make aliasing work in non-interactive scripts.

Upvotes: 2

Related Questions