Answers:
尽管我喜欢jordanm给出的答案,但我认为向经验不足的Linux用户展示如何自行解决此类问题也同样重要。
与在Google搜索结果页面上显示的随机页面上寻找答案相比,建议的方法更快,更通用。
首先,可以在Bash不键入显式路径的情况下运行的所有命令./command都可以分为两类:Bash shell builtins和external commands。Bash shell builtins与一起安装Bash并是它的一部分,而external commands不是一部分Bash。这很重要,因为Bash shell builtins在内部man bash进行了文档记录,并且它们的文档也可以使用help命令来调用,而external commands它们通常是自己记录的,manpages或者带有一些王者-h, --help标记。检查命令是a Bash shell builtin还是an external command:
$ type local
local is a shell builtin
它将显示how command would be interpreted if used as a command name(来自help type)。在这里我们可以看到local是一个shell builtin。让我们看另一个例子:
$ type vim
vim is /usr/bin/vim
在这里,我们可以看到vim不是shell builtin而是一个位于中的外部命令/usr/bin/vim。但是,有时同一命令可以同时安装为external command和shell builtin。添加-a以type列出所有可能性,例如:
$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
在这里我们可以看到echo既是shell builtin和external command。但是,如果您只是键入echo并按下Returna,shell builtin则会调用它,因为它首先出现在此列表中。请注意,所有这些版本echo都不需要相同。例如,在我的系统上/usr/bin/echo带--help标志而a builtin则没有。
好的,现在,当我们知道这local是一个内置的shell时,让我们了解一下它是如何工作的:
$ help local
local: local [option] name[=value] ...
Define local variables.
Create a local variable called NAME, and give it VALUE. OPTION can
be any option accepted by `declare'.
Local variables can only be used within a function; they are visible
only to the function where they are defined and its children.
Exit Status:
Returns success unless an invalid option is supplied, an error occurs,
or the shell is not executing a function.
请注意第一行:name[=value]。[和之间的所有内容]都是可选的。这是世界上许多manpages文档形式使用的通用约定*nix。话虽如此,您在问题中询问的命令是完全合法的。反过来,...字符意味着可以重复前面的参数。您还可以在以下版本中阅读有关此约定的信息man man:
The following conventions apply to the SYNOPSIS section and can be used
as a guide in other sections.
bold text type exactly as shown.
italic text replace with appropriate argument.
[-abc] any or all arguments within [ ] are optional.
-a|-b options delimited by | cannot be used together.
argument ... argument is repeatable.
[expression] ... entire expression within [ ] is repeatable.
因此,总而言之,我希望现在您可以更轻松地了解不同命令的Linux工作方式。
local吗?
man bash。进入那里后,键入/Arrays$以跳到数组部分。(紧随$其后的Arrays是防止循环访问该部分的文本引用。)您可以在此处键入f以前进一页或b后退。完成后,键入q以退出手册页。
help如果您想了解更多信息,则不带参数运行将列出所有bash内置函数。
local只需将变量声明为仅在当前定义的函数中具有作用域,这样主执行环境就无法“看到”该值。您不能local在函数外部使用。例
func() {
nonlocal="Non local variable"
local onlyhere="Local variable"
}
func
echo $nonlocal
echo $onlyhere
输出:非局部变量
因此$onlyhere在函数范围之外不可见。
var=(),但是我想在不知道要查找的名称的情况下要花很多时间。;)