1 ## compare_shells: bash
2
3 #### sh -i
4 # Notes:
5 # - OSH prompt goes to stdout and bash goes to stderr
6 # - This test seems to fail on the system bash, but succeeds with spec-bin/bash
7 echo 'echo foo' | PS1='[prompt] ' $SH --rcfile /dev/null -i >out.txt 2>err.txt
8 fgrep -q '[prompt]' out.txt err.txt
9 echo match=$?
10 ## STDOUT:
11 match=0
12 ## END
13
14 #### \[\] are non-printing
15 PS1='\[foo\]\$'
16 echo "${PS1@P}"
17 ## STDOUT:
18 foo$
19 ## END
20
21 #### literal escapes
22 PS1='\a\e\r\n'
23 echo "${PS1@P}"
24 ## stdout-json: "\u0007\u001b\r\n\n"
25
26 #### special case for $
27 # NOTE: This might be broken for # but it's hard to tell since we don't have
28 # root. Could inject __TEST_EUID or something.
29 PS1='$'
30 echo "${PS1@P}"
31 PS1='\$'
32 echo "${PS1@P}"
33 PS1='\\$'
34 echo "${PS1@P}"
35 PS1='\\\$'
36 echo "${PS1@P}"
37 PS1='\\\\$'
38 echo "${PS1@P}"
39 ## STDOUT:
40 $
41 $
42 $
43 \$
44 \$
45 ## END
46
47 #### PS1 evaluation order
48 x='\'
49 y='h'
50 PS1='$x$y'
51 echo "${PS1@P}"
52 ## STDOUT:
53 \h
54 ## END
55
56 #### PS1 evaluation order 2
57 foo=foo_value
58 dir=$TMP/'$foo' # Directory name with a dollar!
59 mkdir -p $dir
60 cd $dir
61 PS1='\w $foo'
62 test "${PS1@P}" = "$PWD foo_value"
63 echo status=$?
64 ## STDOUT:
65 status=0
66 ## END
67
68 #### \1004
69 PS1='\1004$'
70 echo "${PS1@P}"
71 ## STDOUT:
72 @4$
73 ## END
74
75 #### \001 octal literals are supported
76 PS1='[\045]'
77 echo "${PS1@P}"
78 ## STDOUT:
79 [%]
80 ## END
81
82 #### \555 is beyond max octal byte of \377 and wrapped to m
83 PS1='\555$'
84 echo "${PS1@P}"
85 ## STDOUT:
86 m$
87 ## END
88
89 #### \x55 hex literals not supported
90 PS1='[\x55]'
91 echo "${PS1@P}"
92 ## STDOUT:
93 [\x55]
94 ## END
95
96 #### Single backslash
97 PS1='\'
98 echo "${PS1@P}"
99 ## STDOUT:
100 \
101 ## END
102
103 #### Escaped backslash
104 PS1='\\'
105 echo "${PS1@P}"
106 ## STDOUT:
107 \
108 ## END
109
110 #### \0001 octal literals are not supported
111 PS1='[\0455]'
112 echo "${PS1@P}"
113 ## STDOUT:
114 [%5]
115 ## END
116
117 #### \u0001 unicode literals not supported
118 PS1='[\u0001]'
119 USER=$(whoami)
120 test "${PS1@P}" = "[${USER}0001]"
121 echo status=$?
122 ## STDOUT:
123 status=0
124 ## END
125
126 #### constant string
127 PS1='$ '
128 echo "${PS1@P}"
129 ## STDOUT:
130 $
131 ## END
132
133 #### hostname
134
135 # NOTE: This test is not hermetic. On my machine the short and long host name
136 # are the same.
137
138 PS1='\h '
139 test "${PS1@P}" = "$(hostname -s) " # short name
140 echo status=$?
141 PS1='\H '
142 test "${PS1@P}" = "$(hostname) "
143 echo status=$?
144 ## STDOUT:
145 status=0
146 status=0
147 ## END
148
149 #### username
150 PS1='\u '
151 USER=$(whoami)
152 test "${PS1@P}" = "${USER} "
153 echo status=$?
154 ## STDOUT:
155 status=0
156 ## END
157
158 #### current working dir
159 PS1='\w '
160 test "${PS1@P}" = "${PWD} "
161 echo status=$?
162 ## STDOUT:
163 status=0
164 ## END
165
166 #### \W is basename of working dir
167 PS1='\W '
168 test "${PS1@P}" = "$(basename $PWD) "
169 echo status=$?
170 ## STDOUT:
171 status=0
172 ## END
173
174 #### \t for 24h time (HH:MM:SS)
175 PS1='foo \t bar'
176 echo "${PS1@P}" | egrep -q 'foo [0-2][0-9]:[0-5][0-9]:[0-5][0-9] bar'
177 echo matched=$?
178
179 ## STDOUT:
180 matched=0
181 ## END
182
183 #### \T for 12h time (HH:MM:SS)
184 PS1='foo \T bar'
185 echo "${PS1@P}" | egrep -q 'foo [0-1][0-9]:[0-5][0-9]:[0-5][0-9] bar'
186 echo matched=$?
187
188 ## STDOUT:
189 matched=0
190 ## END
191
192 #### \@ for 12h time (HH:MM AM/PM)
193 PS1='foo \@ bar'
194 echo "${PS1@P}" | egrep -q 'foo [0-1][0-9]:[0-5][0-9] (A|P)M bar'
195 echo matched=$?
196
197 ## STDOUT:
198 matched=0
199 ## END
200
201 #### \A for 24h time (HH:MM)
202 PS1='foo \A bar'
203 echo "${PS1@P}" | egrep -q 'foo [0-2][0-9]:[0-5][0-9] bar'
204 echo matched=$?
205 ## STDOUT:
206 matched=0
207 ## END
208
209 #### \d for date
210 PS1='foo \d bar'
211 echo "${PS1@P}" | egrep -q 'foo [A-Z][a-z]+ [A-Z][a-z]+ [0-9]+ bar'
212 echo matched=$?
213
214 ## STDOUT:
215 matched=0
216 ## END
217
218 #### \D{%H:%M} for strftime
219 PS1='foo \D{%H:%M} bar'
220 echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9] bar'
221 echo matched=$?
222
223 PS1='foo \D{%H:%M:%S} bar'
224 echo "${PS1@P}" | egrep -q 'foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9] bar'
225 echo matched=$?
226
227 ## STDOUT:
228 matched=0
229 matched=0
230 ## END
231
232 #### \D{} for locale specific strftime
233
234 # In bash y.tab.c uses %X when string is empty
235 # This doesn't seem to match exactly, but meh for now.
236
237 PS1='foo \D{} bar'
238 echo "${PS1@P}" | egrep -q '^foo [0-9][0-9]:[0-9][0-9]:[0-9][0-9]( ..)? bar$'
239 echo matched=$?
240 ## STDOUT:
241 matched=0
242 ## END
243
244 #### \s for shell, \v for major.minor version, and \V for full version
245 PS1='foo \s bar'
246 echo "${PS1@P}" | egrep -q '^foo (bash|osh) bar$'
247 echo match=$?
248
249 PS1='foo \v bar'
250 echo "${PS1@P}" | egrep -q '^foo [0-9]+\.[0-9]+ bar$'
251 echo match=$?
252
253 PS1='foo \V bar'
254 echo "${PS1@P}" | egrep -q '^foo [0-9]+\.[0-9]+\.[0-9]+ bar$'
255 echo match=$?
256
257 ## STDOUT:
258 match=0
259 match=0
260 match=0
261 ## END
262
263
264 #### \j for number of jobs
265 set -m # enable job control
266 PS1='foo \j bar'
267 echo "${PS1@P}" | egrep -q 'foo 0 bar'
268 echo matched=$?
269 sleep 5 &
270 echo "${PS1@P}" | egrep -q 'foo 1 bar'
271 echo matched=$?
272 kill %%
273 fg
274 echo "${PS1@P}" | egrep -q 'foo 0 bar'
275 echo matched=$?
276
277 ## STDOUT:
278 matched=0
279 matched=0
280 sleep 5
281 matched=0
282 ## END
283
284 #### \l for TTY device basename
285 PS1='foo \l bar'
286 # FIXME this never an actual TTY when using ./test/spec.sh
287 tty="$(tty)"
288 if [[ "$tty" == "not a tty" ]]; then
289 expected="tty"
290 else
291 expected="$(basename "$tty")"
292 fi
293 echo "${PS1@P}" | egrep -q "foo $expected bar"
294 echo matched=$?
295
296 ## STDOUT:
297 matched=0
298 ## END
299
300 #### \! for history number
301 set -o history # enable history
302 PS1='foo \! bar'
303 history -c # clear history
304 echo "${PS1@P}" | egrep -q "foo 1 bar"
305 echo matched=$?
306 echo "_${PS1@P}" | egrep -q "foo 3 bar"
307 echo matched=$?
308
309 ## STDOUT:
310 matched=0
311 matched=0
312 ## END
313
314 #### \# for command number
315 PS1='foo \# bar'
316 prev_cmd_num="$(echo "${PS1@P}" | egrep -o 'foo [0-9]+ bar' | sed -E 's/foo ([0-9]+) bar/\1/')"
317 echo "${PS1@P}" | egrep -q "foo $((prev_cmd_num + 1)) bar"
318 echo matched=$?
319
320 ## STDOUT:
321 matched=0
322 ## END
323
324 #### @P with array
325 $SH -c 'echo ${@@P}' dummy a b c
326 echo status=$?
327 $SH -c 'echo ${*@P}' dummy a b c
328 echo status=$?
329 $SH -c 'a=(x y); echo ${a@P}' dummy a b c
330 echo status=$?
331 ## STDOUT:
332 a b c
333 status=0
334 a b c
335 status=0
336 x
337 status=0
338 ## END
339
340 #### default PS1
341 #flags='--norc --noprofile'
342 flags='--rcfile /dev/null'
343
344 $SH $flags -i -c 'echo "_${PS1}_"'
345
346 ## STDOUT:
347 _\s-\v\$ _
348 ## END