-->

How to change current working directory inside com

2019-05-07 19:18发布

问题:

I'm trying to write a not found handle in Bash that does the following:

  1. If $1 exists and it's a directory, cd into it.
  2. If $1 exists inside a user defined directory $DEV_DIR, `cd into it.
  3. 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.

回答1:

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.



回答2:

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.



回答3:

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.