Is it possible to override the command line's built in "cd" command?
Solution 1:
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.
Solution 2:
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.