How to undefine a zsh command created accidentally?

This defines two functions, one named grep and the other named vars, whose body is *.py:

grep .vars() *.py                                                         

To remove those functions --- and to unshadow grep --- you want:

$ unset -f grep .vars

From man zshbuiltins:

unset [ -fmv ] name ...
        ...
        unset -f is equivalent to unfunction.
...
unfunction
        Same as unhash -f.
...
unhash
        ...
        The  -f  option causes unhash to remove shell functions.

This is a function definition. More precisely, this is the definition of two functions with the same code. It looks unusual because zsh has several extensions to the basic syntax of function definitions name () { instruction; … }:

  • Zsh allows multiple names. name1 name2 () { instruction; … } defines both the functions name1 and name2 with the same body. I don't know of any other shell that supports this.
  • Zsh allows . in function names (as does for example bash, but not dash or ksh93). Portable function names can only use ASCII letters, digits and underscore.
  • Zsh allows any command as the body of a function. Some shells (in particular bash) require a compound command (which is all that POSIX requires).
    name () { instruction; … }
    
    is the most common form, where the body is a group.
    name () ( instruction; )
    
    is also portable and runs the body of the function in a subshell. Other compound commands are technically valid, for example
    name () if condition; then instruction1; else instruction2; fi
    
    is a POSIX-compliant function definition, but it's extremely unusual in practice.
    name () echo hello
    
    is a perfectly valid function definition according to zsh (and ksh and dash), but not according to POSIX or bash.
  • A space before () is optional in all shells.

To undo the effect of grep .vars() *.py, unset the two functions.

unset -f grep .vars

or

unfunction grep .vars

You've defined a function. This is only valid in the current shell.

A simple function would be something like

f() echo hello

Now the "f" command will say hello

$ f
hello

The simplest way to clear it is with unset -f command.

In your case:

unset -f grep

Tags:

Function

Zsh