Skip to main content

What are commands to find shell keywords, built in functions and user defined functions? [Resolved]

I was discussing with my friend on how the commands are parsed in the shell, and he told me that bash searches the command in following order

  1. List of aliases
  2. List of shell keywords
  3. List of user defined functions
  4. List of shell built in functions
  5. List of directories specified in the PATH variable , from left to right.

I know aliases can be found by issuing the alias command. PATH variable contents can be found using echo $PATH command.

Can you please tell me which commands do I need to use ?

  1. To list all shell keywords
  2. To list all user defined functions
  3. To list of shell built in functions

Question Credit: Forever Learner
Question Reference
Asked January 11, 2019
Posted Under: Unix Linux
4 Answers

In Bash:

  1. man bash | grep -10 RESERVED lists reserved words:

    ! case coproc do done elif else esac fi for function if in select then until while { } time [[ ]]
  2. declare -F and typeset -F shows function names without their contents.
  3. enable lists builtin shell commands (I don't think these are functions as such).So does man builtins

credit: Isaac
Answered January 11, 2019

You can also use compgen in bash:

  • compgen -k lists keywords
  • compgen -b or enable lists builtins
  • compgen -A function or declare -F lists functions
  • compgen -a or alias lists aliases
  • compgen -c lists commands
  • compgen -v lists variables
  • compgen -e or export lists exported variables

credit: user495470
Answered January 11, 2019

The answer for the 2nd question in case of bash or zsh: declare -f.

credit: Renan
Answered January 11, 2019
Your Answer