如何改变内部command_not_found_handle当前工作目录 [英] How to change current working directory inside command_not_found_handle
问题描述
我试着写一击未发现句柄执行以下操作:
I'm trying to write a not found handle in Bash that does the following:
- 如果$ 1成立,这是一个目录,
CD
进去。 - 如果$ 1成立一个用户定义的目录中
$ DEV_DIR
,`cd到它。 - 如果在previous条件不适用,失败。
- If $1 exists and it's a directory,
cd
into it. - If $1 exists inside a user defined directory
$DEV_DIR
, `cd into it. - If the previous conditions don't apply, fail.
现在,我有这样的事情:
Right now I have something like this:
export DEV_DIR=/Users/federico/programacion/
function command_not_found_handle () {
if [ -d $1 ]; then # the dir exists in '.'
cd $1
else
to=$DEV_DIR$1
if [ -d $to ]; then
cd $to
echo `pwd`
else
echo "${1}: command not found"
fi
fi
}
虽然它似乎是工作(即回声PWD
命令打印预期的目录),在实际壳的目录并没有改变。
And although it seems to be working (the echo pwd
command prints the expected dir), the directory in the actual shell does not change.
我是IM pression下,由于这里面有个功能我的的.bashrc
外壳不会叉,我能做的 CD
,但显然这不是工作。关于如何解决这个任何提示将AP preciated。
I was under the impression that since this is a function inside my .bashrc
the shell wouldn't fork and I could do the cd
but apparently that's not working. Any tips on how to solve this would be appreciated.
推荐答案
我的认为的正在发生的事情是,壳叉()
取值设置任何重定向后,但在寻找的命令,所以 command_not_found_handle
不能影响交互shell进程之前。
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.
这篇关于如何改变内部command_not_found_handle当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!