Patch-ID: bash40-021
[bash.git] / examples / functions / whatis
blob56c5a58006f5cc876f3df79f90a3a869cafb444b
2 # whatis -- and implementation of the 10th Edition Unix sh builtin `whatis'
3 #           command.
5 # usage: whatis arg [...]
7 # For each argument, whatis prints the associated value as a parameter,
8 # builtin, function, alias, or executable file as appropriate.  In each
9 # case, the value is printed in a form which would yield the same value
10 # if typed as input to the shell itself.
13 whatis()
15         local wusage='usage: whatis arg [arg...]'
16         local fail=0
18         if [ $# -eq 0 ] ; then
19                 echo "$wusage"
20                 return 1
21         fi
23         for arg
24         do
25                 case $(builtin type -type $arg 2>/dev/null) in
26                 "alias")
27                         builtin alias "$arg"
28                         ;;
29                 "function")
30                         builtin type "$arg" | sed 1d
31                         ;;
32                 "builtin")
33                         echo builtin "$arg"
34                         ;;
35                 "file")
36                         builtin type -path "$arg"
37                         ;;
38                 *)
39                         # OK, we could have a variable, or we could have nada
40                         if [ "$(eval echo \${$arg+set})" = "set" ] ; then
41                                 # It is a variable, and it is set
42                                 echo -n "$arg="
43                                 eval echo '\"'\$$arg'\"'
44                         else
45                                 echo whatis: $arg: not found
46                                 fail=1
47                         fi
48                         ;;
49                 esac
50         done
51         return $fail