How do I get a list of all available shell command

2020-07-09 09:30发布

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

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

标签: bash shell
10条回答
不美不萌又怎样
2楼-- · 2020-07-09 09:50

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

compgen is of course better

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=':' )

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-07-09 09:57

You could use compgen. For example:

compgen -c

You also could grep it, like this:

compgen -c | grep top$

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

查看更多
祖国的老花朵
4楼-- · 2020-07-09 09:58

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. :-)

查看更多
霸刀☆藐视天下
5楼-- · 2020-07-09 10:03

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

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

help complete 
查看更多
登录 后发表回答