Is it possible to override the command line's built in "cd" command?
The following should work:
function cd() { builtin cd "$@" && ls -l; }
Since the function is on a single line, ensure that it is terminated with ;
as above in order to work correctly.
I think you're running into a loop. Your cd
function is calling cd
, which is... the function.
You need to know about builtin
which is a keyword which makes command lookup use the the Bash builtins like cd and not your function
function cd
{
builtin cd "$1"
: do something else
}
Also, calling /usr/bin/cd
will never work, even if such a command existed.
What would happen:
- My Bash shell is in dir
/bash/dir
. - I run a command
/usr/bin/cd /dir/for/cd
. /usr/bin/cd
goes to dir/dir/for/cd
./usr/bin/cd
exits.- Bash is still in
/bash/dir
, because the child process/usr/bin/cd
can not affect the parent.
Also aliases are simple text substitutions. They can never have parameters.
I suggest not to override the cd because there are some other scripts hijacking the 'cd' function, for example, rvm. It would be better to choose another name, like 'd', instead and don't specify the 'builtin' type in your function; otherwise, the hijacker would not function. I am using the code below:
function d() { cd "$@" && ls;}