1 #!/usr/bin/env bash
2 #
3 # printf
4 # bash-completion uses this odd printf -v construction. It seems to mostly use
5 # %s and %q though.
6 #
7 # %s should just be
8 # declare $var='val'
9 #
10 # NOTE:
11 # /usr/bin/printf %q "'" seems wrong.
12 # $ /usr/bin/printf %q "'"
13 # ''\'''
14 #
15 # I suppose it is technically correct, but it looks very ugly.
16
17 #### printf -v %s
18 var=foo
19 printf -v $var %s 'hello there'
20 argv.py "$foo"
21 ## STDOUT:
22 ['hello there']
23 ## END
24 ## N-I mksh/zsh/ash STDOUT:
25 -v['']
26 ## END
27 ## N-I dash STDOUT:
28 ['']
29 ## END
30
31 #### printf -v %q
32 val='"quoted" with spaces and \'
33
34 # quote 'val' and store it in foo
35 printf -v foo %q "$val"
36 # then round trip back to eval
37 eval "bar=$foo"
38
39 # debugging:
40 #echo foo="$foo"
41 #echo bar="$bar"
42 #echo val="$val"
43
44 test "$bar" = "$val" && echo OK
45 ## STDOUT:
46 OK
47 ## END
48 ## N-I mksh/zsh/ash stdout-json: "-v"
49 ## N-I mksh/zsh/ash status: 1
50 ## N-I dash stdout-json: ""
51 ## N-I dash status: 1
52
53 #### printf -v a[1]
54 a=(a b c)
55 printf -v 'a[1]' %s 'foo'
56 echo status=$?
57 argv.py "${a[@]}"
58 ## STDOUT:
59 status=0
60 ['a', 'foo', 'c']
61 ## END
62 ## N-I mksh/zsh STDOUT:
63 -vstatus=0
64 ['a', 'b', 'c']
65 ## END
66 ## N-I dash/ash stdout-json: ""
67 ## N-I dash/ash status: 2
68 ## N-I osh STDOUT:
69 status=2
70 ['a', 'b', 'c']
71 ## END
72
73 #### dynamic declare instead of %s
74 var=foo
75 declare $var='hello there'
76 argv.py "$foo"
77 ## STDOUT:
78 ['hello there']
79 ## END
80 ## N-I dash/mksh/ash STDOUT:
81 ['']
82 ## END
83
84 #### dynamic declare instead of %q
85 var=foo
86 val='"quoted" with spaces and \'
87 # I think this is bash 4.4 only.
88 declare $var="${val@Q}"
89 echo "$foo"
90 ## STDOUT:
91 '"quoted" with spaces and \'
92 ## END
93 ## N-I dash/ash stdout-json: ""
94 ## N-I dash/ash status: 2
95 ## N-I mksh stdout-json: "\n"
96 ## N-I zsh stdout-json: ""
97 ## N-I zsh status: 1
98
99 #### printf -v dynamic scope
100 case $SH in *mksh|*zsh|*dash|*/ash) echo not implemented; exit ;; esac
101 # OK so printf is like assigning to a var.
102 # printf -v foo %q "$bar" is like
103 # foo=${bar@Q}
104 dollar='dollar'
105 f() {
106 local mylocal=foo
107 printf -v dollar %q '$' # assign foo to a quoted dollar
108 printf -v mylocal %q 'mylocal'
109 echo dollar=$dollar
110 echo mylocal=$mylocal
111 }
112 echo dollar=$dollar
113 echo --
114 f
115 echo --
116 echo dollar=$dollar
117 echo mylocal=$mylocal
118 ## STDOUT:
119 dollar=dollar
120 --
121 dollar=\$
122 mylocal=mylocal
123 --
124 dollar=\$
125 mylocal=
126 ## END
127 ## OK osh STDOUT:
128 dollar=dollar
129 --
130 dollar='$'
131 mylocal='mylocal'
132 --
133 dollar='$'
134 mylocal=
135 ## END
136 ## N-I dash/ash/mksh/zsh STDOUT:
137 not implemented
138 ## END
139
140 #### printf with too few arguments
141 printf -- '-%s-%s-%s-\n' 'a b' 'x y'
142 ## STDOUT:
143 -a b-x y--
144 ## END
145
146 #### printf with too many arguments
147 printf -- '-%s-%s-\n' a b c d e
148 ## STDOUT:
149 -a-b-
150 -c-d-
151 -e--
152 ## END
153
154 #### printf width strings
155 printf '[%5s]\n' abc
156 printf '[%-5s]\n' abc
157 ## STDOUT:
158 [ abc]
159 [abc ]
160 ## END
161
162 #### printf integer
163 printf '%d\n' 42
164 printf '%i\n' 42 # synonym
165 printf '[%5d]\n' 42
166 printf '[%-5d]\n' 42
167 printf '[%05d]\n' 42
168 #printf '[%-05d]\n' 42 # the leading 0 is meaningless
169 #[42 ]
170 ## STDOUT:
171 42
172 42
173 [ 42]
174 [42 ]
175 [00042]
176 ## END
177
178 #### printf %6.4d -- precision means something different for integers !?
179 printf '[%6.4d]\n' 42
180 ## STDOUT:
181 [ 0042]
182 ## END
183 ## N-I osh stdout-json: ""
184 ## N-I osh status: 2
185
186 #### printf %6.4s does both truncation and padding
187 printf '[%6s]\n' foo
188 printf '[%6.4s]\n' foo
189 printf '[%-6.4s]\n' foo
190 printf '[%6s]\n' spam-eggs
191 printf '[%6.4s]\n' spam-eggs
192 printf '[%-6.4s]\n' spam-eggs
193 ## STDOUT:
194 [ foo]
195 [ foo]
196 [foo ]
197 [spam-eggs]
198 [ spam]
199 [spam ]
200 ## END
201
202 #### printf %6.0s and %0.0s
203 printf '[%6.0s]\n' foo
204 printf '[%0.0s]\n' foo
205 ## STDOUT:
206 [ ]
207 []
208 ## END
209 ## N-I mksh stdout-json: "[ ]\n["
210 ## N-I mksh status: 1
211
212 #### unsigned / octal / hex
213 printf '[%u]\n' 42
214 printf '[%o]\n' 42
215 printf '[%x]\n' 42
216 printf '[%X]\n' 42
217 ## STDOUT:
218 [42]
219 [52]
220 [2a]
221 [2A]
222 ## END
223
224 #### negative numbers with unsigned / octal / hex
225 printf '[%u]\n' -42
226 printf '[%o]\n' -42
227 printf '[%x]\n' -42
228 printf '[%X]\n' -42
229 ## STDOUT:
230 [18446744073709551574]
231 [1777777777777777777726]
232 [ffffffffffffffd6]
233 [FFFFFFFFFFFFFFD6]
234 ## END
235
236 # osh DISALLOWS this because the output depends on the machine architecture.
237 ## N-I osh stdout-json: ""
238 ## N-I osh status: 1
239
240 #### printf floating point (not required, but they all implement it)
241 printf '[%f]\n' 3.14159
242 printf '[%.2f]\n' 3.14159
243 printf '[%8.2f]\n' 3.14159
244 printf '[%-8.2f]\n' 3.14159
245 printf '[%-f]\n' 3.14159
246 printf '[%-f]\n' 3.14
247 ## STDOUT:
248 [3.141590]
249 [3.14]
250 [ 3.14]
251 [3.14 ]
252 [3.141590]
253 [3.140000]
254 ## END
255 ## N-I osh stdout-json: ""
256 ## N-I osh status: 2
257
258 #### printf floating point with - and 0
259 printf '[%8.4f]\n' 3.14
260 printf '[%08.4f]\n' 3.14
261 printf '[%8.04f]\n' 3.14 # meaning less 0
262 printf '[%08.04f]\n' 3.14
263 echo ---
264 # these all boil down to the same thing. The -, 8, and 4 are respected, but
265 # none of the 0 are.
266 printf '[%-8.4f]\n' 3.14
267 printf '[%-08.4f]\n' 3.14
268 printf '[%-8.04f]\n' 3.14
269 printf '[%-08.04f]\n' 3.14
270 ## STDOUT:
271 [ 3.1400]
272 [003.1400]
273 [ 3.1400]
274 [003.1400]
275 ---
276 [3.1400 ]
277 [3.1400 ]
278 [3.1400 ]
279 [3.1400 ]
280 ## END
281 ## N-I osh STDOUT:
282 ---
283 ## END
284 ## N-I osh status: 2
285
286 #### printf eE fF gG
287 printf '[%e]\n' 3.14
288 printf '[%E]\n' 3.14
289 printf '[%f]\n' 3.14
290 # bash is the only one that implements %F? Is it a synonym?
291 #printf '[%F]\n' 3.14
292 printf '[%g]\n' 3.14
293 printf '[%G]\n' 3.14
294 ## STDOUT:
295 [3.140000e+00]
296 [3.140000E+00]
297 [3.140000]
298 [3.14]
299 [3.14]
300 ## END
301 ## N-I osh stdout-json: ""
302 ## N-I osh status: 2
303
304 #### printf backslash escapes
305 argv.py "$(printf 'a\tb')"
306 argv.py "$(printf '\xE2\x98\xA0')"
307 argv.py "$(printf '\044e')"
308 argv.py "$(printf '\0377')" # out of range
309 ## STDOUT:
310 ['a\tb']
311 ['\xe2\x98\xa0']
312 ['$e']
313 ['\x1f7']
314 ## END
315 ## N-I dash STDOUT:
316 ['a\tb']
317 ['\\xE2\\x98\\xA0']
318 ['$e']
319 ['\x1f7']
320 ## END
321
322 #### printf octal backslash escapes
323 argv.py "$(printf '\0377')"
324 argv.py "$(printf '\377')"
325 ## STDOUT:
326 ['\x1f7']
327 ['\xff']
328 ## END
329
330 #### printf unicode backslash escapes
331 argv.py "$(printf '\u2620')"
332 argv.py "$(printf '\U0000065f')"
333 ## STDOUT:
334 ['\xe2\x98\xa0']
335 ['\xd9\x9f']
336 ## END
337 ## N-I dash/ash STDOUT:
338 ['\\u2620']
339 ['\\U0000065f']
340 ## END
341
342 #### printf invalid backslash escape (is ignored)
343 printf '[\Z]\n'
344 ## STDOUT:
345 [\Z]
346 ## END
347
348 #### printf % escapes
349 printf '[%%]\n'
350 ## STDOUT:
351 [%]
352 ## END
353
354 #### printf %b backslash escaping
355 printf '[%s]\n' '\044' # escapes not evaluated
356 printf '[%b]\n' '\044' # YES, escapes evaluated
357 echo status=$?
358 ## STDOUT:
359 [\044]
360 [$]
361 status=0
362 ## END
363 ## N-I osh STDOUT:
364 [\044]
365 status=2
366 ## END
367
368 #### printf %c -- doesn't respect UTF-8! Bad.
369 twomu=$'\u03bc\u03bc'
370 printf '[%s]\n' "$twomu"
371 printf '%c' "$twomu" | wc --bytes
372 ## STDOUT:
373 [μμ]
374 1
375 ## END
376 ## N-I dash STDOUT:
377 [$\u03bc\u03bc]
378 1
379 ## END
380 ## N-I ash STDOUT:
381 [\u03bc\u03bc]
382 1
383 ## END
384 ## N-I osh STDOUT:
385 [μμ]
386 0
387 ## END
388
389 #### printf invalid format
390 printf '%z' 42
391 echo status=$?
392 printf '%-z' 42
393 echo status=$?
394 ## STDOUT:
395 status=1
396 status=1
397 ## END
398 # osh emits parse errors
399 ## OK osh STDOUT:
400 status=2
401 status=2
402 ## END
403 ## BUG ash STDOUT:
404 status=0
405 status=0
406 ## END
407
408 #### printf %q
409 x='a b'
410 printf '[%q]\n' "$x"
411 ## STDOUT:
412 ['a b']
413 ## END
414 ## OK bash/zsh STDOUT:
415 [a\ b]
416 ## END
417 ## N-I ash/dash stdout-json: "["
418 ## N-I ash/dash status: 1
419
420 #### printf %6q (width)
421 # NOTE: coreutils /usr/bin/printf does NOT implement this %6q !!!
422 x='a b'
423 printf '[%6q]\n' "$x"
424 ## STDOUT:
425 [ 'a b']
426 ## END
427 ## OK bash/zsh STDOUT:
428 [ a\ b]
429 ## END
430 ## N-I mksh/ash/dash stdout-json: "["
431 ## N-I mksh/ash/dash status: 1
432
433 #### printf + and space flags
434 # I didn't know these existed -- I only knew about - and 0 !
435 printf '[%+d]\n' 42
436 printf '[%+d]\n' -42
437 printf '[% d]\n' 42
438 printf '[% d]\n' -42
439 ## STDOUT:
440 [+42]
441 [-42]
442 [ 42]
443 [-42]
444 ## END
445 ## N-I osh stdout-json: ""
446 ## N-I osh status: 2
447
448 #### printf # flag
449 # I didn't know these existed -- I only knew about - and 0 !
450 printf '[%#o]\n' 42
451 printf '[%#x]\n' 42
452 printf '[%#X]\n' 42
453 echo ---
454 printf '[%#f]\n' 3
455 ## STDOUT:
456 [052]
457 [0x2a]
458 [0X2A]
459 ---
460 [3.000000]
461 ## END
462 ## N-I osh STDOUT:
463 ---
464 ## END
465 ## N-I osh status: 2
466
467 #### Runtime error for invalid integer
468 x=3abc
469 printf '%d\n' $x
470 echo status=$?
471 printf '%d\n' xyz
472 echo status=$?
473 ## STDOUT:
474 3
475 status=1
476 0
477 status=1
478 ## END
479 # zsh should exit 1 in both cases
480 ## BUG zsh STDOUT:
481 0
482 status=1
483 0
484 status=0
485 ## END
486 # fails but also prints 0 instead of 3abc
487 ## BUG ash STDOUT:
488 0
489 status=1
490 0
491 status=1
492 ## END
493 # osh doesn't print anything invalid
494 ## OK osh STDOUT:
495 status=1
496 status=1
497 ## END
498
499 #### %(strftime format)T
500 printf '%(%Y-%m-%d)T\n' 1557978599
501 echo status=$?
502 ## STDOUT:
503 2019-05-15
504 status=0
505 ## END
506 ## N-I dash/mksh/zsh/ash STDOUT:
507 status=1
508 ## END
509 ## N-I osh STDOUT:
510 status=2
511 ## END