Is it possible to make some shell calls automatically when entering a directory?

Just like the local .bashrc , which source d every time I enter the directory. How to do it?

+4
source share
2 answers

You can use an alias:

 $ echo 'echo "execute something for $PWD"' > tests/.cdrc $ _cd() { \cd $1 [ -r .cdrc ] && . .cdrc } 

this function will first change to the directory specified as the argument, check if the .cdrc file is .cdrc and enter it.

 $ alias cd=_cd 

Then

 $ cd tests execute something for /path/to/tests 
+7
source

bash and zsh (and possibly many other shells) have a function that allows you to run an arbitrary command before the prompt appears. You can use this for the source of the .dirrc file and it will not break tab completion.

Here's how to do it in bash :

 PROMPT_COMMAND=' if [ "${PREV}" != "$(pwd -P)" ]; then if [ -r .dirrc ]; then . ./.dirrc fi PREV=$(pwd -P) fi ' 

On the bash man page:

PROMPT_COMMAND : if set, the value is executed as a command before each initial prompt is PROMPT_COMMAND .

Here's how to do it in zsh (see the zshmisc man page):

 precmd() { if [ "${PREV}" != "$(pwd -P)" ]; then if [ -r .dirrc ]; then . ./.dirrc fi PREV=$(pwd -P) fi } 
+3
source

Source: https://habr.com/ru/post/1486342/


All Articles