4 # these should output error messages -- the format is required
8 # these should output nothing
12 # in the future this may mean to put the output into VAR, but for
14 # 2005-03-15 no longer an error
16 printf -v var "%10d" $RANDOM
19 # this should expand escape sequences in the format string, nothing else
22 # this should not cut off output after the \c
25 # and unrecognized backslash escapes should have the backslash preserverd
28 printf "no newline " ; printf "now newline\n"
33 # this was a bug caused by pre-processing the string for backslash escapes
34 # before doing the `%' format processing -- all versions before bash-2.04
38 # simple character output
41 # test simple string output
42 printf "%s\n" unquoted
44 # test quoted string output
45 printf "%s %q\n" unquoted quoted
46 printf "%s%10q\n" unquoted quoted
48 printf "%q\n" 'this&that'
50 # make sure the format string is reused to use up arguments
51 printf "%d " 1 2 3 4 5; printf "\n"
53 # make sure that extra format characters get null arguments
54 printf "%s %d %d %d\n" onestring
56 printf "%s %d %u %4.2f\n" onestring
58 printf -- "--%s %s--\n" 4.2 ''
59 printf -- "--%s %s--\n" 4.2
63 # 8 is a non-octal digit, so the `81' should be output
64 printf -- "--%b--\n" '\n\081'
66 printf -- "--%b--\n" '\t\0101'
67 printf -- "--%b--\n" '\t\101'
69 # these should all display `A7'
73 printf "%b\n" '\01017'
77 printf -- "--%b--\n" '\"abcd\"'
78 printf -- "--%b--\n" "\'abcd\'"
80 printf -- "--%b--\n" 'a\\x'
82 printf -- "--%b--\n" '\x'
84 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
87 if [ "$Z1" != "$Z2" ]; then
88 echo "whoops: printf %b and $'' differ" >&2
92 printf -- "--%b--\n" ''
95 # the stuff following the \c should be ignored, as well as the rest
96 # of the format string
97 printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
99 # unrecognized escape sequences should by displayed unchanged
100 printf -- "--%b--\n" '4\.2'
102 # a bare \ should not be processed as an escape sequence
103 printf -- "--%b--\n" '\'
105 # make sure extra arguments are ignored if the format string doesn't
108 printf " " 4.4 BSD ; printf "\n"
110 # make sure that a fieldwidth and precision of `*' are handled right
111 printf "%10.8s\n" 4.4BSD
112 printf "%*.*s\n" 10 8 4.4BSD
114 printf "%10.8q\n" 4.4BSD
115 printf "%*.*q\n" 10 8 4.4BSD
117 printf "%6b\n" 4.4BSD
118 printf "%*b\n" 6 4.4BSD
120 # we handle this crap with homemade code in printf.def
121 printf "%10b\n" 4.4BSD
122 printf -- "--%-10b--\n" 4.4BSD
123 printf "%4.2b\n" 4.4BSD
124 printf "%.3b\n" 4.4BSD
125 printf -- "--%-8b--\n" 4.4BSD
127 # test numeric conversions -- these four lines should echo identically
128 printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
129 printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
131 printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
132 printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
138 printf "%*d\n" 10 -42
140 # test some simple floating point formats
142 printf "%#4.2f\n" 4.2
143 printf "%#4.1f\n" 4.2
145 printf "%*.*f\n" 4 2 4.2
146 printf "%#*.*f\n" 4 2 4.2
147 printf "%#*.*f\n" 4 1 4.2
159 # test some of the more esoteric features of POSIX.1 printf
160 printf "%d\n" "'string'"
161 printf "%d\n" '"string"'
163 printf "%#o\n" "'string'"
164 printf "%#o\n" '"string"'
166 printf "%#x\n" "'string'"
167 printf "%#X\n" '"string"'
169 printf "%6.2f\n" "'string'"
170 printf "%6.2f\n" '"string"'
172 # output from these two lines had better be the same
173 printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
174 printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
177 printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
178 printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
180 # tests for translating \' to ' and \\ to \
181 # printf translates \' to ' in the format string...
184 # but not when the %b format specification is used
185 printf "%b\n" \\\'abcd\\\'
187 # but both translate \\ to \
189 printf "%b\n" '\\abcd\\'
191 # this was reported as a bug in bash-2.03
192 # these three lines should all echo `26'
199 # this should be an overflow, but error messages vary between systems
200 # printf "%lu\n" 4294967296
202 # ...but we cannot use this because some systems (SunOS4, for example),
203 # happily ignore overflow conditions in strtol(3)
204 #printf "%ld\n" 4294967296
209 # this caused an infinite loop in older versions of printf
212 # these should print a warning and `0', according to POSIX.2
216 # failures in all bash versions through bash-2.05
220 printf '%.0b-%.0s\n' foo bar
221 printf '(%*b)(%*s)\n' -4 foo -4 bar
223 format='%'`printf '%0100384d' 0`'d\n'
226 # failures in all bash versions through bash-3.0 - undercounted characters
228 printf " %s %s %s \n%n" ab cd ef vv
231 # this doesn't work with printf(3) on all systems
234 # test cases from an austin-group list discussion
235 # prints ^G as an extension
239 printf '%b\n' '\0007'
244 # prints no more than two hex digits
247 # additional backslash escapes
250 # failures with decimal precisions until after bash-3.1
257 # failures with various floating point formats and 0 after bash-3.2
289 printf "%08X\n" 2604292517
291 # make sure these format specifiers all output '' for empty string arguments
304 # bug in bash versions up to and including bash-3.2
306 printf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)'
307 shopt -s nullglob extglob
308 echo "x$(printf "%b" @(hugo))x"
309 printf -v var "%b" @(hugo); echo "x${var}x"
311 ${THIS_SH} ./printf2.sub