Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customizing cd command


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.

like image 234
peeyush Avatar asked Apr 05 '12 10:04

peeyush


1 Answers

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
}
like image 157
Rory Hunter Avatar answered Sep 30 '22 13:09

Rory Hunter