Skip to content

Instantly share code, notes, and snippets.

@joshenders
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshenders/f0c0cba5187eba1bc790 to your computer and use it in GitHub Desktop.
Save joshenders/f0c0cba5187eba1bc790 to your computer and use it in GitHub Desktop.
rvm/extras/bash_zsh_support/chpwd/function.sh
__zsh_like_cd()
{
# Called as:
# cd() { __zsh_like_cd cd "$@" ; }
# popd() { __zsh_like_cd popd "$@" ; }
# pushd() { __zsh_like_cd pushd "$@" ; }
# So, "$@" in __zsh_like_cd contains the __zsh_like_cd "personality" as well as passed arguments
\typeset __zsh_like_cd_hook # define local variable in __zsh_like_cd scope, may be POSIXly correct but lol- #! is /bin/bash, use "local __zsh_like_cd_hook". Also, hilarious that they prepend \ to typeset because who knows if you can trust that it's not hooked!
if
builtin "$@" # builtin returns true if command is a built-in
then
shift || true # remove the called "personality", such as cd, pushd, or popd
for __zsh_like_cd_hook in chpwd "${chpwd_functions[@]}" # no idea where chpwd is defined, git grep "function chpwd" returns nothing. Apparently this means something in zsh but this is definitely supposed to executed with bash, in fact, this whole function is hacking a zsh feature into bash
do
if \typeset -f "$__zsh_like_cd_hook" >/dev/null 2>&1 # if the hooked binary is a shell function
then "$__zsh_like_cd_hook" "$@" || break # call each hooked shell function with passed arguments and break on first failed function call
fi
done
true # override return value of last hooked shell function
else
return $? # return value of $(builtin "$@") for some reason
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment