For compgen
(and I think for Linux in general), a command is everything that you can run including alias' and functions, etc.
If you want to get only executable files/scripts in any of your $PATH
directories, there is no good way using compgen
.
You could use comm
to show all commands excluding aliases, keywords and functions:
comm -23 \
<(compgen -c | sort -u) \
<(compgen -akA function | sort -u)
However, this will also remove commands that are both alias/function and command (e.g. ls
or grep
have an alias per default in Ubuntu, as well as anything you added yourself).
So, I think you're better off getting all executables from $PATH
with your own script (and if you wish you can add builtins
using compgen -b
):
path_filenames(){
printf '%s' "$PATH" \
| xargs -d: -I{} -- find -L {} -maxdepth 1 -executable -type f -printf '%P\n' 2>/dev/null
}
sort -u <(path_filenames) <(compgen -b)
compgen -a
to return aliasescompgen -c
instead shown also functions and keywords