1 #!/bin/bash
2 #
3 # Bash implements type -t
4
5 #### type -t -> function
6 f() { echo hi; }
7 type -t f
8 ## stdout: function
9
10 #### type -t -> alias
11 shopt -s expand_aliases
12 alias foo=bar
13 type -t foo
14 ## stdout: alias
15
16 #### type -t -> builtin
17 type -t echo read : [ declare local break continue
18 ## STDOUT:
19 builtin
20 builtin
21 builtin
22 builtin
23 builtin
24 builtin
25 builtin
26 builtin
27 ## END
28
29 #### type -t -> keyword
30 type -t for time ! fi do {
31 ## STDOUT:
32 keyword
33 keyword
34 keyword
35 keyword
36 keyword
37 keyword
38 ## END
39
40 #### type -t -> file
41 type -t find xargs
42 ## STDOUT:
43 file
44 file
45 ## END
46
47 #### type -t doesn't find non-executable (like command -v)
48 PATH="$TMP:$PATH"
49 touch $TMP/non-executable
50 type -t non-executable
51 ## stdout-json: ""
52 ## status: 1
53 ## BUG bash STDOUT:
54 file
55 ## END
56 ## BUG bash status: 0
57
58 #### type -t -> not found
59 type -t echo ZZZ find =
60 echo status=$?
61 ## STDOUT:
62 builtin
63 file
64 status=1
65 ## END
66
67 #### help
68 help
69 help help
70 ## status: 0
71
72 #### bad help topic
73 help ZZZ 2>$TMP/err.txt
74 echo "help=$?"
75 cat $TMP/err.txt | grep -i 'no help topics' >/dev/null
76 echo "grep=$?"
77 ## STDOUT:
78 help=1
79 grep=0
80 ## END
81
82 #### type -p builtin -> file
83 type -p mv tar grep
84 ## STDOUT:
85 /bin/mv
86 /bin/tar
87 /bin/grep
88 ## END
89
90 #### type -p builtin -> not found
91 type -p FOO BAR NOT_FOUND
92 ## status: 1
93 ## stdout-json: ""
94
95 #### type -p builtin -> not a file
96 type -p cd type builtin command
97 ## stdout-json: ""
98
99 #### type -P builtin -> file
100 type -P mv tar grep
101 ## STDOUT:
102 /bin/mv
103 /bin/tar
104 /bin/grep
105 ## END
106
107 #### type -P builtin -> not found
108 type -P FOO BAR NOT_FOUND
109 ## status: 1
110 ## stdout-json: ""
111
112 #### type -P builtin -> not a file
113 type -P cd type builtin command
114 ## stdout-json: ""
115 ## status: 1
116
117 #### type -P builtin -> not a file but file found
118 mv () { ls; }
119 tar () { ls; }
120 grep () { ls; }
121 type -P mv tar grep cd builtin command type
122 ## status: 1
123 ## STDOUT:
124 /bin/mv
125 /bin/tar
126 /bin/grep
127 ## END
128
129 #### type -f builtin -> not found
130 type -f FOO BAR NOT FOUND
131 ## status: 1
132
133 #### type -f builtin -> function and file exists
134 mv () { ls; }
135 tar () { ls; }
136 grep () { ls; }
137 type -f mv tar grep
138 ## STDOUT:
139 /bin/mv is a file
140 /bin/tar is a file
141 /bin/grep is a file
142 ## OK bash STDOUT:
143 mv is /bin/mv
144 tar is /bin/tar
145 grep is /bin/grep