T. Rex T. Rex - 6 months ago 12
Bash Question

ZSH: Call in-built function from zsh function that uses the same name

I use zsh and would like to slightly extend the in-built

cd
function.
I'd like that when I call
cd
, it changes directly and then lists the content of the directory.

function cd() {
cd $1
ls .
}


I'd have expected this code to work, but as it turns out, the call to
cd
refers to the function definition, resulting in an infinite loop.

Is there a work-around to solve this problem, apart from choosing a different name for my function?

UPDATE

Strangely enough, this worked

function cd() {
`echo $1`
ls .
}


No idea why.

Answer

Why the echo command works is because you probably have the autocd option on. You can check this by typing setopt to get your list of options.

Then the echo-ing of the directory name and catching the output triggered the autocd and you went to that directory.

Comments