5


Generally I keep directory specific settings in .bashrc and whenever I change directory execute the command source .bashrc to make those settings effective.
Now I was thinking of manipulating cd command in ~/.bashrc, so whenever I cd to new directory and if any .bashrc exists there, it will be loaded automatically.

Similar to this cd $1; source .bashrc ( I have verified that $1 is valid path), but problem is cd is shell builting, so it's a recursive loop ( cd always points to modifed cd ). We do not have elf file of cd ( which generally we have of other commands viz scp or others). So how can I achieve this ? Also if shopt -s cdspell is supported then also I need to have cd spelled path in argument of $1.

peeyush
  • 2,841
  • 3
  • 24
  • 43
  • Why People are closing it ? Is this duplicate to some other thread , I don't think so. Or because of people having 100K or 900K reputation couldn't get chance to answer it ? – peeyush Apr 07 '12 at 05:37

4 Answers4

6

You want the "builtin" command;

builtin shell-builtin [arguments]

Execute the specified shell builtin, passing it arguments, and return its exit status. This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function. The cd builtin is commonly redefined this way. The return status is false if shell-builtin is not a shell builtin command.

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

So, you could have something like (untested, don't have a bash handy either);

function cd() {
    builtin cd $1 \
        && test -e .bashrc \
        && source .bashrc
}
Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
2

You might check out direnv. https://github.com/zimbatm/direnv

rlduffy
  • 608
  • 4
  • 8
  • One of the specific goals when I designed direnv was to avoid overriding builtin commands as RVM does (did?). Instead direnv depends on shell hooks like PROMPT_COMMAND in bash to detect directory changes. – zimbatm Dec 02 '14 at 13:50
1

RVM does this:

$ 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
}

And yes, this works on my machine. Essentially, as @RoryHunter said, use builtin and run some code if it succeeds, or return the exit code if it fails.

l0b0
  • 55,365
  • 30
  • 138
  • 223
0

You could try this:

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

Didn't tested this much, however.

Msonic
  • 1,456
  • 15
  • 25
begemotv2718
  • 868
  • 6
  • 14
  • problem is same, recursive call to this new definition of cd. – peeyush Apr 05 '12 at 10:34
  • I've tested this in the following variant: function cdd(){ cd $1; if [ -e ./.bashrc ]; then echo "Hi, there!"; fi } alias cd='cdd' Then prompt> cd Hi, there – begemotv2718 Apr 05 '12 at 10:40