2
compgen -c | sort -b | uniq | less

Shows also functions and keywords (luckily no aliases), I instead want all that are not keywords, functions or aliases. In few words only the commands.

If command name match with function name, keyword name or alias name; the command name must not be omitted.

2
  • Please read this first. For example compgen -a to return aliases – Raffa Mar 2 at 12:24
  • compgen -c instead shown also functions and keywords – Mario Palumbo Mar 2 at 13:07
3

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)
16
  • I have declared a function named sudo and another named apt. With this method, commands that are also functions are omitted. – Mario Palumbo Mar 2 at 15:00
  • True, also things like ls won't show because they also have an alias in Ubuntu. See the alternative, that should work better for you – pLumo Mar 2 at 15:27
  • Yes. This is a perfect approach, not for exclusion but for inclusion. I have found a simile answers that I not have understand. echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \ -executable -type f -printf '%P\n' – Mario Palumbo Mar 2 at 15:30
  • 1
    your solution misses the -L which is important, because many files in $PATH are actually symlinks. – pLumo Mar 2 at 15:33
  • 1
    with comm -2 it works also. – pLumo Mar 2 at 17:57
0

To print aliases, run:

alias

To print functions, run:

declare -F

or

declare -f

To print commands ( excluding aliases, keywords and functions ), run:

compgen -c | \
grep -wv "$(alias | grep -oP '\w+(?=\=)')"| \
grep -wv "$(declare -F | awk '{print $3}')" | \
grep -wFv "$(compgen -k)"

  • grep -wv "$(alias | grep -oP '\w+(?=\=)')" will exclude aliases.
  • grep -wv "$(declare -F | awk '{print $3}')" will exclude functions.
  • grep -wFv "$(compgen -k)" will exclude keywords.
12
  • I want to show all commands and not functions, aliases or keywords. Sorry for bad explain. – Mario Palumbo Mar 2 at 14:07
  • @MarioPalumbo I updated the answer with that. – Raffa Mar 2 at 15:07
  • Aliases do not already appear in the default command compgen -c, the thing that unfortunately appears are the keywords, which have not been removed with your command. – Mario Palumbo Mar 2 at 15:14
  • @MarioPalumbo I updated the last command to exclude keywords as well. – Raffa Mar 2 at 15:33
  • 1
    I think this has the same problem as my first solution then, it removes commands that are also aliases (e.g. ls) or functions. – pLumo Mar 2 at 15:42

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.