What can I type at my shell (which happens to be bash) that will list all the commands that are recognized?

Also, does this differ by shell? Or do all shells just have a "directory" of commands they recognize?

Secondly, different question, but how can I override any of those? In other words how can I write my own view command to replace the one existing on my Ubuntu system, which appears to just load vim.


You can usecompgen

compgen -c # will list all the commands you could run.


compgen -a # will list all the aliases you could run.
compgen -b # will list all the built-ins you could run.
compgen -k # will list all the keywords you could run.
compgen -A function # will list all the functions you could run.
compgen -A function -abck # will list all the above in one go.
| improve this answer | |
  • 6
    Note that compgen -c will include things like { or while that are not strictly speaking commands. – Stéphane Chazelas Jul 28 '15 at 11:41

A shell knows four kinds of commands.

  • Aliases: these are nicknames for a command with some options. They are defined in the shell's initialization file (~/.bashrc for bash).
  • Functions: they are snippets of shell code given a name. Like aliases, they are defined in the shell's initialization file.
  • Builtins: the shell comes with a small number of built-in commands. Most builtins manipulate the shell state (cd changes the current directory, set changes options and positional parameters, export changes the environment, …). Most shell offer largely the same builtins but each shell has a few extensions to the basic set.
  • External commands: they are independent of the shell. Like other programs, the shell executes external programs by looking them up in the executable search path. The PATH environment variable contains a colon-separated list of directories to search for programs.

In case there are commands of several types by the same name, the first match in the order above is executed¹.

You can see what type of command a name corresponds to by running type some_name.

You can list aliases by running the alias built-in with no argument. There is no way to list functions or builtins that works in all shells. You can find a list of builtins in the shell's documentation.

In bash, the set builtin lists functions with their definitions as well as variables. In bash, ksh or zsh, typeset -f lists functions with their definitions. In bash, you can list all command names of any type with compgen -c. You can use compgen -A alias, compgen -A builtin compgen -A function to list commands of a specific type. You can pass an additional string to compgen to list only commands that start with that prefix.

In zsh, you can list the currently available commands of a given type with echo ${(k)aliases}, echo ${(k)functions}, echo ${(k)builtins} and echo ${(k)commands} (that last one lists external commands only).

The following shell-agnostic snippet lists all available external programs:

case "$PATH" in
  (*[!:]:) PATH="$PATH:" ;;

set -f; IFS=:
for dir in $PATH; do
  set +f
  [ -z "$dir" ] && dir="."
  for file in "$dir"/*; do
    if [ -x "$file" ] && ! [ -d "$file" ]; then
      printf '%s = %s\n' "${file##*/}" "$file"

There is an edge case in Bash: hashed commands.

Bash Reference Manual says:

A full search of the directories in $PATH is performed only if the command is not found in the hash table


set -h
mkdir ~/dir-for-wat-command
echo 'echo WAT!' >~/dir-for-wat-command/wat
chmod +x ~/dir-for-wat-command/wat
hash -p ~/dir-for-wat-command/wat wat

The PATH environment variable doesn't contain ~/dir-for-wat-command, compgen -c doesn't show wat, but you can run wat.

If you want to shadow an existing command, define an alias or a function.

¹ Exception: a few builtins (called special builtins) cannot be shadowed by a function — bash and zsh don't comply with POSIX on that point in their default mode though.

| improve this answer | |
  • Another edge case is for values of $PATH like /bin:/usr/bin: (trailing :), where it would fail to list the executable files in the current directory. – Stéphane Chazelas Jul 28 '15 at 11:45

Try this, using :

printf '%s\n' ${PATH//:/\/* }
| improve this answer | |

The list of commands consists of two sets:

  1. The commands built-in to the shell
  2. The commands in your PATH

You can't change built-ins, but you can keep the shell from using a built-in by specifying the full pathname like /bin/echo.

As for the commands in your PATH, you can change that. PATH is a colon-separated list of directories to look for commands in. The first matching file found "wins".

export PATH=~/bin:${PATH}

(This syntax does not work for csh-derived shells, but all others that I know about).

That exports PATH with ~/bin as the first entry followed by the rest of the existing PATH entries. So now your shell will check directory calledbinin your home directory for a command *first* before checking the standardPATH` the system set up. You could easily make it check the new directory last by instead using:

export PATH=${PATH}:~/bin

To make the PATH persist for future logins, add it to a .profile or other shell rc file.

| improve this answer | |

If you press tab key twice and then y, you will get the list of all available command in your current shell. For the second question I think you should use alias: shell alias.

| improve this answer | |
  • regarding alias, is that a fool-proof way of overriding and un-overriding (by "unaliasing") a command when it is called anywhere on the system (for example from a shell script)? – themirror Oct 12 '13 at 17:39
  • 1
    What shell do you use? I can't get the double tab to list all commands on my bash. – terdon Oct 12 '13 at 17:46
  • I use bash on Ubuntu 13.04. There is also a command called compgen which can be used to get the list of all available commands on the bash. – coffeMug Oct 12 '13 at 17:51
  • @terdon Do you have bash completion installed? – coffeMug Oct 12 '13 at 17:53
  • 1
    set show-all-if-ambiguous off – coffeMug Oct 12 '13 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.