bash 如何删除/移除 shell 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:46:28  来源:igfitidea点击:

How do I delete/remove a shell function?

bashshellzsh

提问by too much php

I have done this:

我已经这样做了:

$ z() { echo 'hello world'; }

How do I get rid of it?

我该如何摆脱它?

回答by Robert Gamble

unset -f z

Will unset the function named z. A couple people have answered with:

将取消设置名为 z 的函数。有几个人回答说:

unset z

but if you have a function and a variable named z only the variable will be unset, not the function.

但是如果你有一个函数和一个名为 z 的变量,只有变量将被取消设置,而不是函数。

回答by Micah Elliott

In Zsh:

在 Zsh 中:

unfunction z

That's another (arguably better) name for unhash -f zor unset -f zand is consistent with the rest of the family of:

这是unhash -f zor的另一个(可以说是更好的)名称,unset -f z并且与以下家族的其他成员一致:

  • unset
  • unhash
  • unalias
  • unlimit
  • unsetopt
  • unset
  • unhash
  • unalias
  • unlimit
  • unsetopt

When in doubt with such things, type un<tab>to see the complete list.

如果对此类事情有疑问,请键入un<tab>以查看完整列表。

(Slightly related: It's also nice to have functions/aliases like realiases, refunctions, resetopts, reenv, etc to "re-source" respective files, if you've separated/grouped them as such.)

(略相关:这也是不错的功能/别名一样realiasesrefunctionsresetoptsreenv,等来“重新source”相应的文件,如果你已经分离/组合它们的方式。)