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