1 |
#!/bin/bash |
2 |
|
3 |
#### command -v |
4 |
myfunc() { echo x; } |
5 |
command -v echo |
6 |
echo $? |
7 |
command -v myfunc |
8 |
echo $? |
9 |
command -v nonexistent # doesn't print anything |
10 |
echo $? |
11 |
command -v for |
12 |
echo $? |
13 |
## STDOUT: |
14 |
echo |
15 |
0 |
16 |
myfunc |
17 |
0 |
18 |
1 |
19 |
for |
20 |
0 |
21 |
## OK dash STDOUT: |
22 |
echo |
23 |
0 |
24 |
myfunc |
25 |
0 |
26 |
127 |
27 |
for |
28 |
0 |
29 |
## END |
30 |
|
31 |
#### command -v with multiple names |
32 |
# ALL FOUR SHELLS behave differently here! |
33 |
# |
34 |
# bash chooses to swallow the error! We agree with zsh if ANY word lookup |
35 |
# fails, then the whole thing fails. |
36 |
|
37 |
myfunc() { echo x; } |
38 |
command -v echo myfunc ZZZ for |
39 |
echo status=$? |
40 |
|
41 |
## STDOUT: |
42 |
echo |
43 |
myfunc |
44 |
for |
45 |
status=1 |
46 |
## BUG bash STDOUT: |
47 |
echo |
48 |
myfunc |
49 |
for |
50 |
status=0 |
51 |
## BUG dash STDOUT: |
52 |
echo |
53 |
status=0 |
54 |
## OK mksh STDOUT: |
55 |
echo |
56 |
myfunc |
57 |
status=1 |
58 |
## END |
59 |
|
60 |
#### command -v doesn't find non-executable file |
61 |
# PATH resolution is different |
62 |
|
63 |
PATH="$TMP:$PATH" |
64 |
touch $TMP/non-executable $TMP/executable |
65 |
chmod +x $TMP/executable |
66 |
|
67 |
command -v non-executable | grep -o /non-executable |
68 |
echo status=$? |
69 |
|
70 |
command -v executable | grep -o /executable |
71 |
echo status=$? |
72 |
|
73 |
## STDOUT: |
74 |
status=1 |
75 |
/executable |
76 |
status=0 |
77 |
## END |
78 |
|
79 |
## BUG bash/dash STDOUT: |
80 |
/non-executable |
81 |
status=0 |
82 |
/executable |
83 |
status=0 |
84 |
## END |
85 |
|
86 |
#### command skips function lookup |
87 |
seq() { |
88 |
echo "$@" |
89 |
} |
90 |
command # no-op |
91 |
seq 3 |
92 |
command seq 3 |
93 |
# subshell shouldn't fork another process (but we don't have a good way of |
94 |
# testing it) |
95 |
( command seq 3 ) |
96 |
## STDOUT: |
97 |
3 |
98 |
1 |
99 |
2 |
100 |
3 |
101 |
1 |
102 |
2 |
103 |
3 |
104 |
## END |
105 |
|
106 |
#### command command seq 3 |
107 |
command command seq 3 |
108 |
## STDOUT: |
109 |
1 |
110 |
2 |
111 |
3 |
112 |
## END |
113 |
## N-I zsh stdout-json: "" |
114 |
## N-I zsh status: 127 |
115 |
|
116 |
#### command command -v seq |
117 |
seq() { |
118 |
echo 3 |
119 |
} |
120 |
command command -v seq |
121 |
## stdout: seq |
122 |
## N-I zsh stdout-json: "" |
123 |
## N-I zsh status: 127 |
124 |
|
125 |
#### history usage |
126 |
history |
127 |
echo status=$? |
128 |
history +5 # hm bash considers this valid |
129 |
echo status=$? |
130 |
history -5 # invalid flag |
131 |
echo status=$? |
132 |
history f |
133 |
echo status=$? |
134 |
history too many args |
135 |
echo status=$? |
136 |
## status: 0 |
137 |
## STDOUT: |
138 |
status=0 |
139 |
status=0 |
140 |
status=2 |
141 |
status=2 |
142 |
status=2 |
143 |
## END |
144 |
## OK bash STDOUT: |
145 |
status=0 |
146 |
status=0 |
147 |
status=2 |
148 |
status=1 |
149 |
status=1 |
150 |
## END |
151 |
## BUG zsh/mksh STDOUT: |
152 |
status=1 |
153 |
status=1 |
154 |
status=1 |
155 |
status=1 |
156 |
status=1 |
157 |
## END |
158 |
## N-I dash STDOUT: |
159 |
status=127 |
160 |
status=127 |
161 |
status=127 |
162 |
status=127 |
163 |
status=127 |
164 |
## END |
165 |
|
166 |
#### $(command type ls) |
167 |
type() { echo FUNCTION; } |
168 |
type |
169 |
s=$(command type echo) |
170 |
echo $s | grep builtin > /dev/null |
171 |
echo status=$? |
172 |
## STDOUT: |
173 |
FUNCTION |
174 |
status=0 |
175 |
## END |
176 |
## N-I zsh STDOUT: |
177 |
FUNCTION |
178 |
status=1 |
179 |
## END |
180 |
## N-I mksh STDOUT: |
181 |
status=1 |
182 |
## END |
183 |
|
184 |
#### builtin |
185 |
cd () { echo "hi"; } |
186 |
cd |
187 |
builtin cd / && pwd |
188 |
unset -f cd |
189 |
## STDOUT: |
190 |
hi |
191 |
/ |
192 |
## END |
193 |
## N-I dash STDOUT: |
194 |
hi |
195 |
## END |
196 |
|
197 |
#### builtin ls not found |
198 |
builtin ls |
199 |
## status: 1 |
200 |
## N-I dash status: 127 |
201 |
|
202 |
#### builtin no args |
203 |
builtin |
204 |
## status: 0 |
205 |
## N-I dash status: 127 |
206 |
|
207 |
#### builtin command echo hi |
208 |
builtin command echo hi |
209 |
## status: 0 |
210 |
## stdout: hi |
211 |
## N-I dash status: 127 |
212 |
## N-I dash stdout-json: "" |