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-json: "builtin\nbuiltin\nbuiltin\nbuiltin\nbuiltin\nbuiltin\nbuiltin\nbuiltin\n"
19
20 #### type -t builtin -> keyword
21 type -t for time ! fi do {
22 ## stdout-json: "keyword\nkeyword\nkeyword\nkeyword\nkeyword\nkeyword\n"
23
24 #### type -t builtin -> file
25 type -t find xargs
26 ## stdout-json: "file\nfile\n"
27
28 #### type -t builtin -> not found
29 type -t echo ZZZ find =
30 echo status=$?
31 ## stdout-json: "builtin\nfile\nstatus=1\n"
32
33 #### help
34 help
35 help help
36 ## status: 0
37
38 #### bad help topic
39 help ZZZ 2>$TMP/err.txt
40 echo "help=$?"
41 cat $TMP/err.txt | grep -i 'no help topics' >/dev/null
42 echo "grep=$?"
43 ## stdout-json: "help=1\ngrep=0\n"
44
45 #### type -p builtin -> file
46 type -p mv tar grep
47 ## STDOUT:
48 /bin/mv
49 /bin/tar
50 /bin/grep
51 ## END
52
53 #### type -p builtin -> not found
54 type -p FOO BAR NOT_FOUND
55 ## status: 1
56 ## stdout-json: ""
57
58 #### type -p builtin -> not a file
59 type -p cd type builtin command
60 ## stdout-json: ""
61
62 #### type -P builtin -> file
63 type -P mv tar grep
64 ## STDOUT:
65 /bin/mv
66 /bin/tar
67 /bin/grep
68 ## END
69
70 #### type -P builtin -> not found
71 type -P FOO BAR NOT_FOUND
72 ## status: 1
73 ## stdout-json: ""
74
75 #### type -P builtin -> not a file
76 type -P cd type builtin command
77 ## stdout-json: ""
78 ## status: 1
79
80 #### type -P builtin -> not a file but file found
81 mv () { ls; }
82 tar () { ls; }
83 grep () { ls; }
84 type -P mv tar grep cd builtin command type
85 ## status: 1
86 ## STDOUT:
87 /bin/mv
88 /bin/tar
89 /bin/grep
90 ## END
91
92 #### type -f builtin -> not found
93 type -f FOO BAR NOT FOUND
94 ## status: 1
95
96 #### type -f builtin -> function and file exists
97 mv () { ls; }
98 tar () { ls; }
99 grep () { ls; }
100 type -f mv tar grep
101 ## STDOUT:
102 /bin/mv is a file
103 /bin/tar is a file
104 /bin/grep is a file
105 ## OK bash STDOUT:
106 mv is /bin/mv
107 tar is /bin/tar
108 grep is /bin/grep