bash 如何获取所有可用 shell 命令的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9960465/
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-18 01:55:42  来源:igfitidea点击:

How do I get a list of all available shell commands

bashshell

提问by Dave Halter

In a typical Linux shell (bash) it is possible to to hit tab twice, to get a list of all available shell commands.

在典型的 Linux shell (bash) 中,可以按两次 Tab 键来获取所有可用 shell 命令的列表。

Is there a command which has the same behaviour? I want to pipe it into grepand search it.

是否有具有相同行为的命令?我想把它导入grep并搜索它。

回答by msbrogli

You could use compgen. For example:

你可以使用compgen。例如:

compgen -c

You also could grep it, like this:

你也可以 grep 它,像这样:

compgen -c | grep top$

Source: http://www.cyberciti.biz/open-source/command-line-hacks/compgen-linux-command/

来源:http: //www.cyberciti.biz/open-source/command-line-hacks/compgen-linux-command/

回答by DigitalRoss

You can list the directories straight from $PATHif you tweak the field separator first. The parens limit the effect to the one command, so use: (...) | grep ...

$PATH如果您首先调整字段分隔符,则可以直接列出目录。括号将效果限制为一个命令,因此请使用:(...) | grep ...

(IFS=': '; ls -1 $PATH)

回答by P.P

"tab" twice & "y" prints all files in the paths of $PATH. So just printing all files in PATH is sufficient.

"tab" 两次 & "y" 打印 $PATH 路径中的所有文件。所以只需打印 PATH 中的所有文件就足够了。

Just type this in the shell:

只需在 shell 中输入:

 # printf "%s\n" ${PATH//:/\/* } > my_commands

This redirect all the commands to a file "my_commands".

这会将所有命令重定向到文件“my_commands”。

回答by ganesshkumar

List all the files in your PATH variable(ls all the directories in the PATH). The default user and system commands will be in /bin and /sbin respectively but on installing some software we will add them to some directory and link it using PATH variable.

列出您的PATH variable(ls 路径中的所有目录)中的所有文件。默认的用户和系统命令将分别位于 /bin 和 /sbin 中,但在安装某些软件时,我们会将它们添加到某个目录并使用 PATH 变量进行链接。

回答by ghoti

There may be things on your path which aren't actually executable.

您的路径上可能存在实际上不可执行的内容。

#!/bin/sh
for d in ${PATH//:/ }; do
  for f in "$d"/*; do
    test -x "$f" && echo -n "$f "
  done
done
echo ""

This will also print paths, of course. If you only want unqualified filenames, it should be easy to adapt this.

当然,这也将打印路径。如果您只想要不合格的文件名,那么调整它应该很容易。

Funny, StackOverflow doesn't know how to handle syntax highlighting for this. :-)

有趣的是,StackOverflow 不知道如何为此处理语法突出显示。:-)

回答by Bohemian

tabtaby

tabtaby

回答by Barton Chittenden

Similar to @ghoti, but using find:

类似于@goti,但使用查找:

#!/bin/sh
for d in ${PATH//:/ }; do
    find $d -maxdepth 1 -type f -executable
done

回答by ams

Bash uses a builtin command named 'complete' to implement the tab feature.

Bash 使用名为“完成”的内置命令来实现选项卡功能。

I don't have the details to hand, but the should tell you all you need to know:

我没有手头的细节,但应该告诉你所有你需要知道的:

help complete 

回答by Tomasz Elendt

(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename {} \; | sort | uniq)

It doesn't include shell builtins though.

虽然它不包括 shell 内置函数。

回答by Dave Halter

An answer got deleted, I liked it most, so I'm trying to repost it:

一个答案被删除了,我最喜欢它,所以我试图重新发布它:

compgen is of course better

compgen当然更好

echo $PATH | tr ':' '\n' | xargs -n 1 ls -1

I found this to be the most typical shell thing, I think it works also with other shells (which I doubt with things like IFS=':')

我发现这是最典型的外壳程序,我认为它也适用于其他外壳程序(我对此表示怀疑IFS=':'

Clearly, there maybe problems, if the file is not an executable, but I think for my question, that is enough - I just want to grepmy output - which means searching for some commands.

显然,如果文件不是可执行文件,则可能存在问题,但我认为对于我的问题,grep这就足够了 - 我只想输出 - 这意味着搜索一些命令。