Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

4 Answers 4

up vote 1 down vote accepted

In Bash:

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

    ! case 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
share|improve this answer

With zsh:

PATH= type -m '*'

Will tell you all 3.

In bash, to list the keywords, you can do:

complete -A keyword :

and then type : <Tab><Tab>

For builtins, replace keyword with builtin above and for functions, I'll let you guess.

share|improve this answer
    
In zsh, the list of aliases, functions and builtins is also available as array keys: print -l ${(ko)aliases}, etc. –  Gilles Jan 30 '13 at 23:11
    
@Stephane Chazelas : Thanks. But when I ran commands complte -A keyword : <tab> <tab> and complte -A builtin : <tab> <tab> , both times it prompted my "Display all 2154 possibilities? (y or n) –  CppLearner Feb 1 '13 at 20:51
    
@CppLearner, that's not what I said. Enter the "complete" command, which redefines the completion for the ":" command, and then type :, space, tab and tab. –  Stéphane Chazelas Feb 1 '13 at 21:58

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

share|improve this answer
    
thanks , that is helpful.+1. –  CppLearner Feb 1 '13 at 20:52

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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