4

Is there a way to list all available commands in terminal?

I know linux has compgen -c is there an equivalent command for OS X?

Update: The actual issue was that I was using zsh instead of bash. So from zsh, I could either just open bash or run a bash command from zsh bash -c "compgen -c"

5
  • 3
    Assuming your shell is bash then compgen -c will work.
    – fd0
    Commented Jun 30, 2016 at 20:52
  • 1
    maybe it is because I am using zsh I am getting zsh: command not found: compgen Commented Jul 1, 2016 at 15:28
  • yep, if I enter bash then compgen -c it works, I guess my question should have been what it the zsh equivalent to the bash command compgen -c Commented Jul 1, 2016 at 15:33
  • so I think if you are using zsh and have tweaked your default shell on a Mac then you must already be quite an advanced user :) Commented Jul 2, 2016 at 10:49
  • I'm starring your question, because even though it was "is there a way to list..." it is always interesting to come back & see what other people are using the command line for on OSX Commented Jul 2, 2016 at 10:50

5 Answers 5

10

In zsh you need to enable the completion system. Please read through man zshcompsys. To install the completion system enter the command

compinstall

and follow the directions. Check the fpath variable

echo $fpath

in my situation I needed to declare the variable in my .zshrc

fpath=(/usr/local/share/zsh/5.0.7/functions)

This is the location of my zsh functions. After the compinit line in your .zshrc add the following lines

autoload -Uz bashcompinit
bashcompinit

This is the completions entries in my .zshrc

#\\\_ COMPLETIONS _///#
zstyle :compinstall filename '/Users/fd0/.zshrc'
zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
autoload -Uz compinit
compinit
autoload -Uz bashcompinit
bashcompinit

I can now enter the command compgen -c and it behaves similar to bash.

6

Just tap the Tab key twice (TabTab). You'll be prompted if you want to see all possible commands. Tap y and you'll be presented with a list.

You can do that same thing for individual commands to see all options for that specific command.

2
  • Good note, but this only works for me if I already have a partial string in place and even then it's a bit different:tab x 1 displays if there are a few options, if there are a lot I have to confirm; more tab strikes cycles through options (maybe my zsh version 5.8 or configuration are different?). In any case, compgen is command that generates completions as output. tab sequences are key bindings, not an "equivalent command".
    – benrifkah
    Commented Apr 26, 2022 at 16:43
  • This doesn't work in Z shell (native config) and I've been unable to configure it so. It used to iirc in bash tho... That said, in Z shell you can use the which or whence or where commands followed by a <space> then hit the <tab> key to get prompted to see all possible listings (via &PATH)
    – MmmHmm
    Commented Jul 11, 2022 at 3:59
4

Like fd0 commented above, the best source for quickly listing most everything you can do in the Terminal, is by using the Bash builtin compgen.

Compgen interfaces with the "completion" function in Bash, so it is intended to keep a list of most everything you can do in the shell. So it is interesting that technically compgen is listing commands, aliases, and functions, as well as a few other things like builtins, variables, groups, jobs and service names. My experience is that it will not list everthing, like applications typcially ran in the GUI, which I will mention below. You can use the open command in OSX for quite a few things, for example.

compgen -c executes the command in a subshell environment, and its output is used as the possible completions, which is probably what you want. If Bash if your shell, it will spew out similar information for all commands or a single command if you press Tab Tab.

It's also common in OSX that many programs can be executed from the shell, so many program names that you'll find in /Applications can also be called via a CLI from the shell. Similarly, many programs are (at least) at /Applications/Utilities, some interesting stuff is at /System/Library/CoreServices/Applications (as well as other things under the /System main directory...), and as well sometimes custom programs get put under the classic Unix locations like /usr/bin, usr/local/bin, /opt, and /sys/bin.

Often you can find other interesting things to type, probably also covered above, by typing help or help [command or argument], with more information about most programs or builtins coming with info [command] or man [command]. Apropos is also quite interesting, which you may know already. These things will get most everything, I imagine there are other things one just needs, or should, hack around and discover...

-1

While in Terminal, type Tab Tab. Then you'll see:

Display all 1653 possibilities? (y or n).

Press Y and it will list all possible commands

2
  • In my case, pressing tab inserts tab. So this probably depends on the Terminal being used.
    – trusktr
    Commented Apr 29, 2019 at 5:45
  • How is this answer different from the answer I left 2 years before your answer?
    – fsb
    Commented Mar 15, 2020 at 4:39
-1

(Tab, Tab) does the trick, but it only works on bash, if it doesn't work firstly just type 'bash' then (Tab, Tab) then you have an option of yes(y) or no(n) then you are fine.

1
  • Welcome to Ask Different. Instead of repeating the same info from two previous answers, it would've been best to edit one of them with 'just type bash'.
    – fsb
    Commented Mar 15, 2020 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .