“which alias”和“whereis alias”不返回任何内容

Bli*_*vie 0 command-line alias coreutils

我最近安装了 Ubuntu 20.04。我找不到别名命令。当我进入时which alias我什么也没有得到。与 相同whereis alias

Eli*_*gan 5

alias是 shell 内置的,所以which找不到whereis它。要找出某命令是什么类型,以及它的可执行文件所在的位置(如果有的话)(即,如果它是外部命令),您可以使用type

$ type alias
alias is a shell builtin
Run Code Online (Sandbox Code Playgroud)

type也是一个 shell 内置函数:

$ type type
type is a shell builtin
Run Code Online (Sandbox Code Playgroud)

当您运行type外部命令(作为 shell 中的单独可执行文件存在的命令)时type,将为您提供有关该命令的详细信息。例如,which它本身就是一个外部命令:

$ type which
which is /usr/bin/which
Run Code Online (Sandbox Code Playgroud)

因为which是外部命令,所以它不知道 shell 提供的命令,在 Bourne 风格的 shell 中是 shell 内置命令和 shell 关键字。

它也不知道您自己创建的 shell 函数或别名的命令。例如:

$ f() { printf 'Hello, world.\n'; }
$ which f
$ type f
f is a function
f ()
{
    printf 'Hello, world.\n'
}
Run Code Online (Sandbox Code Playgroud)
$ alias g="printf 'Hello, world.\n'"
$ which g
$ type g
g is aliased to `printf 'Hello, world.\n''
Run Code Online (Sandbox Code Playgroud)

此外,还可以有一个外部命令,该命令作为某种其他类型的命令提供,例如 shell 内置命令。然后,外部命令被其他命令“隐藏”。运行which将向您显示外部命令,即使这不是从您的 shell 实际运行的命令。例如,Bash 提供了printf内置的:

$ which printf
/usr/bin/printf
$ type printf
printf is a shell builtin
Run Code Online (Sandbox Code Playgroud)

同样,外部命令可以被函数或别名隐藏。您很可能有一个ls扩展为外部命令调用的别名ls

$ which ls
/usr/bin/ls
$ type ls
ls is aliased to `ls --color=auto'
Run Code Online (Sandbox Code Playgroud)

所有 Bourne 风格的 shell 都有一个type命令,可以按上述任何示例中所示的方式使用该命令。一些 Bourne 风格的 shell(包括 Bash)还允许您传递选项,-a以便type它显示您所指定名称的所有命令。例如:

$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
Run Code Online (Sandbox Code Playgroud)

该输出的含义是,当您ls从 shell 运行时,它正在运行一个扩展为 的别名ls --color=auto,但如果您没有该别名(或者要删除它),则将ls运行可执行文件/usr/bin/ls,并且如果不存在,那么它将运行可执行文件/bin/ls。(在我的系统上,这两个可执行文件实际上是同一个文件,可以通过不同的路径访问,但 shell 不知道也不关心这一点。)您的输出可能会有所不同。

当使用 Bash 或其他 Bourne 风格的 shell 时,您通常应该使用type 代替which,因为它可以为您提供更可靠的信息。如果某些东西是外部命令,您更喜欢仅查看路径(即以提供的样式查看输出which),则可能需要使用或commmand -v来代替。如果正确,则给出相同的输出:typewhichwhichcommand -v

$ type file
file is /usr/bin/file
$ which file
/usr/bin/file
$ command -v file
/usr/bin/file
Run Code Online (Sandbox Code Playgroud)

如果which出现错误(相对于您正在使用的 shell 的实际行为),command -v仍然不会误导您:

$ type ls
ls is aliased to `ls --color=auto'
$ which ls
/usr/bin/ls
$ command -v ls
alias ls='ls --color=auto'
Run Code Online (Sandbox Code Playgroud)

相比之下,即使该whereis命令不了解 shell 中的任何内容,也没有任何一个命令通常应该优于whereis. 原因是它whereis不仅向您显示外部命令所在的位置。它还尝试向您显示与该命令相关的其他一些重要文件的位置。如果这就是您正在寻找的内容,您可以同时运行两者typewhereis以避免被误导。在大多数情况下,跑步只会type告诉你需要知道的事情。

最后,关于alias您询问的命令:由于alias是 shell 内置命令,因此它没有自己的手册页。在大多数 Ubuntu 系统上, runningman alias与 running 具有相同的效果man bash_builtins,显示 Bash 内置命令的列表,但不提供有关其中任何一个的具体信息。

要获取有关 Bash 中 shell 内置命令和 shell 关键字的帮助,可以使用该help命令,该命令本身就是 shell 内置命令。help alias将向您显示有关的帮助aliashelp help将向您展示其本身的帮助help。(也可以看看help type。)

您可能还会发现Bash 参考手册以及Bash 联机帮助页(您可以通过运行查看man bash)很有用。它们有记录各种 shell 内置函数和 shell 关键字的部分。