Skip to content

Instantly share code, notes, and snippets.

@Joopmicroop
Last active January 5, 2018 19:58
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 Joopmicroop/3bd1db6da2cbb5e13b40abdb0d2a02d8 to your computer and use it in GitHub Desktop.
Save Joopmicroop/3bd1db6da2cbb5e13b40abdb0d2a02d8 to your computer and use it in GitHub Desktop.
run script on cd'ing into and out of a directory
# onCD
# -----
# Place the code below in your .bashrc file.
# then place a script in any directory, named 'oncd.sh'
# the script will receive an argument of 'enter' or 'leave'
# when cd'ing into or out of the directory.
#(doesn't work with "shopt -s autocd", use COMMAND_PROMPT for that)
cd () {
oldpath=$PWD
builtin cd "$@" && oncd $oldpath
}
# these need more work
# pushd () { builtin pushd "$@" && oncd "enter" }
# popd () { builtin popd "$@" && oncd "enter" }
oncd () {
#echo "oldpath: $1"
[[ -f "$1/oncd.sh" ]] && sh $1/oncd.sh "leave"
[[ -f "oncd.sh" ]] && sh oncd.sh "enter"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment