How to change current working directory inside command_not_found_handle

ⅰ亾dé卋堺 提交于 2019-12-05 16:05:14

I think what's going on is that the shell fork()s after setting up any redirections but before looking for commands, so command_not_found_handle can't affect the interactive shell process.

What you seem to want to do may partly possible using the autocd feature:

shopt -s autocd

From man bash:

autocd - If set, a command name that is the name of a directory is executed as if it were the argument to the cd com‐ mand. This option is only used by interactive shells.

Otherwise, just create a function that you invoke by name that performs the actions you are trying to use command_not_found_handle for.

It won't change directies if you run this program as a script in your main shell because it creates a sub-shell when it executes. If you source the script in your current shell then it will have the desired effect.

~/wbailey> source command_not_found.sh

That said, I think the following would achieve the same result:

wesbailey@feynman:~/code_katas> cd xxx 2> /dev/null || cd ..; pwd
/Users/wesbailey

just replace the ".." with your env var defined directory and create an alias in your .bashrc file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!