Say I have this bash function:
cd(){
builtin cd "$@"
echo "do my own thing"
}
the problem is that if I override cd
in another place, only 1 override will count, any other override will be overwritten by the latest sourced function!
Is there a way to have multiple cd
functions and for them to all be called?
I am thinking something like this:
alias prev_cd="cd";
cd(){
prev_cd
echo "do my own thing"
}
but I doubt that's quite right..the problem with the prev_cd
solution is that it will result in an infinite loop. Where prev_cd calls my cd function, and vice versa. I only want prev_cd to be defined as cd definitions outside of this file..
One trick I can use is I can find the file that the cd function is located in using:
shopt -s extdebug
declare -F cd
shopt -u extdebug
if I have already overridden the cd
command, I can call unset -f cd
?
It looks like RVM (Ruby Version Manager) overrides cd
:
cd 14 /Users/alexamil/.rvm/scripts/cd
so I don't want to mess up previous overrides of cd
..so I only want to override cd
if I don't mess up the previous overrides...
cd
is to call__zsh_like_cd cd
and that function__zsh_like_cd
will be in your environment... So maybe just using that in your own redefinition ofcd
might be OK... It relies on RVM not changing the rules, but might be good enough... But see below for a more general answer.