]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/printf.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / printf.tests
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
cce855bc 14LC_ALL=C
bb70624e 15LC_NUMERIC=C
cce855bc
JA
16
17# these should output error messages -- the format is required
18printf
19printf --
20
21# these should output nothing
22printf ""
23printf -- ""
24
95732b49
JA
25# in the future this may mean to put the output into VAR, but for
26# now it is an error
27# 2005-03-15 no longer an error
28unset var
29printf -v var "%10d" $RANDOM
30echo ${#var}
31
cce855bc
JA
32# this should expand escape sequences in the format string, nothing else
33printf "\tone\n"
34
35# this should not cut off output after the \c
36printf "one\ctwo\n"
37
38# and unrecognized backslash escapes should have the backslash preserverd
39printf "4\.2\n"
40
41printf "no newline " ; printf "now newline\n"
42
43# %% -> %
44printf "%%\n"
45
bb70624e
JA
46# this was a bug caused by pre-processing the string for backslash escapes
47# before doing the `%' format processing -- all versions before bash-2.04
48printf "\045" ; echo
49printf "\045d\n"
50
cce855bc
JA
51# simple character output
52printf "%c\n" ABCD
53
54# test simple string output
55printf "%s\n" unquoted
56
57# test quoted string output
58printf "%s %q\n" unquoted quoted
59printf "%s%10q\n" unquoted quoted
60
61printf "%q\n" 'this&that'
62
63# make sure the format string is reused to use up arguments
64printf "%d " 1 2 3 4 5; printf "\n"
65
66# make sure that extra format characters get null arguments
67printf "%s %d %d %d\n" onestring
68
69printf "%s %d %u %4.2f\n" onestring
70
71printf -- "--%s %s--\n" 4.2 ''
72printf -- "--%s %s--\n" 4.2
73
74# test %b escapes
75
76# 8 is a non-octal digit, so the `81' should be output
77printf -- "--%b--\n" '\n\081'
78
79printf -- "--%b--\n" '\t\0101'
80printf -- "--%b--\n" '\t\101'
81
82# these should all display `A7'
0628567a 83echo -e "\01017"
f73dda09 84echo -e "\x417"
cce855bc
JA
85
86printf "%b\n" '\01017'
87printf "%b\n" '\1017'
f73dda09 88printf "%b\n" '\x417'
cce855bc
JA
89
90printf -- "--%b--\n" '\"abcd\"'
91printf -- "--%b--\n" "\'abcd\'"
92
93printf -- "--%b--\n" 'a\\x'
94
95printf -- "--%b--\n" '\x'
96
97Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
98Z2=$'\a\b\e\f\r\v'
99
100if [ "$Z1" != "$Z2" ]; then
101 echo "whoops: printf %b and $'' differ" >&2
102fi
103unset Z1 Z2
104
105printf -- "--%b--\n" ''
106printf -- "--%b--\n"
107
108# the stuff following the \c should be ignored, as well as the rest
109# of the format string
110printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
111
bb70624e
JA
112# unrecognized escape sequences should by displayed unchanged
113printf -- "--%b--\n" '4\.2'
114
115# a bare \ should not be processed as an escape sequence
116printf -- "--%b--\n" '\'
117
b72432fd
JA
118# make sure extra arguments are ignored if the format string doesn't
119# actually use them
120printf "\n" 4.4 BSD
121printf " " 4.4 BSD ; printf "\n"
122
cce855bc
JA
123# make sure that a fieldwidth and precision of `*' are handled right
124printf "%10.8s\n" 4.4BSD
125printf "%*.*s\n" 10 8 4.4BSD
126
127printf "%10.8q\n" 4.4BSD
128printf "%*.*q\n" 10 8 4.4BSD
129
130printf "%6b\n" 4.4BSD
131printf "%*b\n" 6 4.4BSD
132
133# we handle this crap with homemade code in printf.def
134printf "%10b\n" 4.4BSD
135printf -- "--%-10b--\n" 4.4BSD
136printf "%4.2b\n" 4.4BSD
137printf "%.3b\n" 4.4BSD
138printf -- "--%-8b--\n" 4.4BSD
139
140# test numeric conversions -- these four lines should echo identically
141printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
142printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
143
144printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
145printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
146
147printf "%10d\n" 42
148printf "%10d\n" -42
149
150printf "%*d\n" 10 42
151printf "%*d\n" 10 -42
152
153# test some simple floating point formats
154printf "%4.2f\n" 4.2
155printf "%#4.2f\n" 4.2
156printf "%#4.1f\n" 4.2
157
158printf "%*.*f\n" 4 2 4.2
159printf "%#*.*f\n" 4 2 4.2
160printf "%#*.*f\n" 4 1 4.2
161
162printf "%E\n" 4.2
163printf "%e\n" 4.2
164printf "%6.1E\n" 4.2
165printf "%6.1e\n" 4.2
166
167printf "%G\n" 4.2
168printf "%g\n" 4.2
169printf "%6.2G\n" 4.2
170printf "%6.2g\n" 4.2
171
172# test some of the more esoteric features of POSIX.1 printf
173printf "%d\n" "'string'"
174printf "%d\n" '"string"'
175
176printf "%#o\n" "'string'"
177printf "%#o\n" '"string"'
178
179printf "%#x\n" "'string'"
180printf "%#X\n" '"string"'
181
182printf "%6.2f\n" "'string'"
183printf "%6.2f\n" '"string"'
184
185# output from these two lines had better be the same
186printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
187printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
188
189# and these two also
190printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
191printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
192
bb70624e
JA
193# tests for translating \' to ' and \\ to \
194# printf translates \' to ' in the format string...
195printf "\'abcd\'\n"
196
197# but not when the %b format specification is used
198printf "%b\n" \\\'abcd\\\'
199
200# but both translate \\ to \
201printf '\\abcd\\\n'
202printf "%b\n" '\\abcd\\'
203
204# this was reported as a bug in bash-2.03
205# these three lines should all echo `26'
206printf "%d\n" 0x1a
207printf "%d\n" 032
208printf "%d\n" 26
209
cce855bc
JA
210# error messages
211
212# this should be an overflow, but error messages vary between systems
213# printf "%lu\n" 4294967296
214
215# ...but we cannot use this because some systems (SunOS4, for example),
216# happily ignore overflow conditions in strtol(3)
217#printf "%ld\n" 4294967296
218
cce855bc
JA
219printf "%10"
220printf "ab%Mcd\n"
221
222# this caused an infinite loop in older versions of printf
223printf "%y" 0
224
f73dda09 225# these should print a warning and `0', according to POSIX.2
cce855bc
JA
226printf "%d\n" GNU
227printf "%o\n" GNU
f73dda09
JA
228
229# failures in all bash versions through bash-2.05
230printf "%.0s" foo
231printf "%.*s" 0 foo
232
233printf '%.0b-%.0s\n' foo bar
234printf '(%*b)(%*s)\n' -4 foo -4 bar
235
236format='%'`printf '%0100384d' 0`'d\n'
237printf $format 0
238
95732b49
JA
239# failures in all bash versions through bash-3.0 - undercounted characters
240unset vv
241printf " %s %s %s \n%n" ab cd ef vv
242echo "$vv"
243
f73dda09
JA
244# this doesn't work with printf(3) on all systems
245#printf "%'s\n" foo
b80f6443
JA
246
247# test cases from an austin-group list discussion
248# prints ^G as an extension
249printf '%b\n' '\7'
250
251# prints ^G
252printf '%b\n' '\0007'
253
254# prints NUL then 7
255printf '\0007\n'
256
257# prints no more than two hex digits
258printf '\x07e\n'
259
260# additional backslash escapes
261printf '\"\?\n'
0628567a
JA
262
263# failures with decimal precisions until after bash-3.1
264printf '%0.5d\n' 1
265
266printf '%05d\n' 1
267printf '%5d\n' 1
268printf '%0d\n' 1
3185942a
JA
269
270# failures with various floating point formats and 0 after bash-3.2
271
272printf "%G\n" 0
273printf "%g\n" 0
274printf "%4.2G\n" 0
275printf "%4.2g\n" 0
276
277printf "%G\n" 4
278printf "%g\n" 4
279printf "%4.2G\n" 4
280printf "%4.2g\n" 4
281
282printf "%F\n" 0
283printf "%f\n" 0
284printf "%4.2F\n" 0
285printf "%4.2f\n" 0
286
287printf "%F\n" 4
288printf "%f\n" 4
289printf "%4.2F\n" 4
290printf "%4.2f\n" 4
291
292printf "%E\n" 0
293printf "%e\n" 0
294printf "%4.2E\n" 0
295printf "%4.2e\n" 0
296
297printf "%E\n" 4
298printf "%e\n" 4
299printf "%4.2E\n" 4
300printf "%4.2e\n" 4
301
302printf "%08X\n" 2604292517
303
304# make sure these format specifiers all output '' for empty string arguments
305echo q
306printf "%q\n" ""
307printf "%q\n"
308
309echo s
310printf "%s\n" ''
311printf "%s\n"
312
313echo b
314printf "%b\n" ''
315printf "%b\n"
316
317# bug in bash versions up to and including bash-3.2
318v=yyy
319printf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)'
320shopt -s nullglob extglob
321echo "x$(printf "%b" @(hugo))x"
322printf -v var "%b" @(hugo); echo "x${var}x"
0001803f 323
d233b485
CR
324# make sure that missing arguments are always handled like the empty string
325printf "<%3s><%3b>\n"
326
495aee44
CR
327# tests variable assignment with -v
328${THIS_SH} ./printf1.sub
329
0001803f 330${THIS_SH} ./printf2.sub
495aee44
CR
331
332${THIS_SH} ./printf3.sub
ac50fbac
CR
333
334${THIS_SH} ./printf4.sub