Cd command setup


Typically, I save the directory-specific settings to .bashrc, and whenever I change the directory, run the command source .bashrcto make these settings effective.
 Now I was thinking about manipulating the cd command in ~/.bashrc, so whenever I connect to a new directory and if there is any .bashrc, it will load automatically.

Similar to this cd $1; source .bashrc(I checked that $ 1 is a valid path), but the problem in cd is building a shell, so this is a recursive loop (cd always points to modifed cd). We do not have an ef cd file (which usually has other commands: scp or others). So how can I achieve this? Also, if supported shopt -s cdspell, then I also need to have the cd spelled path in the $ 1 argument.

+5
source share
4 answers

You need the builtin command

built-in shell-builtin [arguments]

Run the specified shell by passing its arguments and returning its exit status. This is useful when defining a function whose name matches the name of the inline shell while maintaining the functionality of the inline function. A built-in CD is usually redefined in this way. The return status is false if shell-builtin is not a shell built-in command.

From: http://linux.die.net/man/1/bash

That way you might have something like (untested, no and bash);

function cd() {
    builtin cd $1 \
        && test -e .bashrc \
        && source .bashrc
}
+6
source

You can check direnv. https://github.com/zimbatm/direnv

+2
source

RVM :

$ type cd
cd is a function
cd () 
{ 
    if builtin cd "$@"; then
        [[ -n "${rvm_current_rvmrc:-}" && "$*" == "." ]] && rvm_current_rvmrc="" || true;
        __rvm_do_with_env_before;
        __rvm_project_rvmrc;
        __rvm_after_cd;
        __rvm_do_with_env_after;
        return 0;
    else
        return $?;
    fi
}

, . , @RoryHunter, builtin , , , .

+1
source

You can try the following:

function cdd(){ cd $1; if [ -e ./.bashrc ] ; then source ./.bashrc; fi; }
alias cd = 'cdd' 
?

However, this has not been verified.

0
source

All Articles