How can I create a function in zsh that calls an existing command with the same name?

How can I write a function in zsh that invokes an existing command with the same name as the function itself? For example, I’ve tried this to illustrate my question:

function ls 
{
    ls -l $1 $2 $3
}

When I execute it with ls * I get the following:

ls:1: maximum nested function level reached

I assume this is because the function is being called recursively. How I can avoid that?

This is a crude example, and in this case an alias would do the job, but I have a more complex example where an alias isn’t suitable and so I would need to write a function.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What is happening is that you are recursively calling your ls function. In order to use the binary, you can use ZSH’s command builtin.

function ls {
    command ls -l "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="052145">[email protected]</a>"
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x