I'm trying to write a not found handle in Bash that does the following:
- 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
}
And although it seems to be working (the echo pwd
command prints the expected dir), the directory in the actual shell does not change.
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.
cd ~/prog; cd something
gets a bit tiring after some time. I'd love to hear any other suggestions to doing this. – Mowery