Bash execute shell function after change directory (cd)

Trying to find a way to execute a function in BASH after changing to a directory.

eg,

# cd code/project/blah "Your latest modified files are main.cc blah.hpp blah.cc" (~/code/project/blah) # _ 

With the above, I hope that I will have the opportunity to wrap other functions around the BASH command.

I wanted to find something by the lines of the zsh hook functions http://zsh.sourceforge.net/Doc/Release/Functions.html#SEC45

+4
source share
4 answers

Don't forget about pushd and popd if you never use them. I would do this:

 PS1='(\w) \$ ' chdir() { local action="$1"; shift case "$action" in # popd needs special care not to pass empty string instead of no args popd) [[ $# -eq 0 ]] && builtin popd || builtin popd "$*" ;; cd|pushd) builtin $action "$*" ;; *) return ;; esac # now do stuff in the new pwd echo Your last 3 modified files: ls -t | head -n 3 } alias cd='chdir cd' alias pushd='chdir pushd' alias popd='chdir popd' 
+4
source

You can make an alias for cd that performs standard cd , and then a specific function that you defined.

+3
source

Use PROMPT_COMMAND . You should take a look at git -sh, which includes many git fancy tricks, and should also be easy to learn.

+3
source
 alias cd='echo DO SOME WORK; cd $*' 
0
source

All Articles