4 # these should output error messages -- the format is required
8 # these should output nothing
12 # this should expand escape sequences in the format string, nothing else
15 # this should not cut off output after the \c
18 # and unrecognized backslash escapes should have the backslash preserverd
21 printf "no newline " ; printf "now newline\n"
26 # this was a bug caused by pre-processing the string for backslash escapes
27 # before doing the `%' format processing -- all versions before bash-2.04
31 # simple character output
34 # test simple string output
35 printf "%s\n" unquoted
37 # test quoted string output
38 printf "%s %q\n" unquoted quoted
39 printf "%s%10q\n" unquoted quoted
41 printf "%q\n" 'this&that'
43 # make sure the format string is reused to use up arguments
44 printf "%d " 1 2 3 4 5; printf "\n"
46 # make sure that extra format characters get null arguments
47 printf "%s %d %d %d\n" onestring
49 printf "%s %d %u %4.2f\n" onestring
51 printf -- "--%s %s--\n" 4.2 ''
52 printf -- "--%s %s--\n" 4.2
56 # 8 is a non-octal digit, so the `81' should be output
57 printf -- "--%b--\n" '\n\081'
59 printf -- "--%b--\n" '\t\0101'
60 printf -- "--%b--\n" '\t\101'
62 # these should all display `A7'
66 printf "%b\n" '\01017'
68 printf "%b\n" '\x0417'
70 printf -- "--%b--\n" '\"abcd\"'
71 printf -- "--%b--\n" "\'abcd\'"
73 printf -- "--%b--\n" 'a\\x'
75 printf -- "--%b--\n" '\x'
77 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
80 if [ "$Z1" != "$Z2" ]; then
81 echo "whoops: printf %b and $'' differ" >&2
85 printf -- "--%b--\n" ''
88 # the stuff following the \c should be ignored, as well as the rest
89 # of the format string
90 printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
92 # unrecognized escape sequences should by displayed unchanged
93 printf -- "--%b--\n" '4\.2'
95 # a bare \ should not be processed as an escape sequence
96 printf -- "--%b--\n" '\'
98 # make sure extra arguments are ignored if the format string doesn't
101 printf " " 4.4 BSD ; printf "\n"
103 # make sure that a fieldwidth and precision of `*' are handled right
104 printf "%10.8s\n" 4.4BSD
105 printf "%*.*s\n" 10 8 4.4BSD
107 printf "%10.8q\n" 4.4BSD
108 printf "%*.*q\n" 10 8 4.4BSD
110 printf "%6b\n" 4.4BSD
111 printf "%*b\n" 6 4.4BSD
113 # we handle this crap with homemade code in printf.def
114 printf "%10b\n" 4.4BSD
115 printf -- "--%-10b--\n" 4.4BSD
116 printf "%4.2b\n" 4.4BSD
117 printf "%.3b\n" 4.4BSD
118 printf -- "--%-8b--\n" 4.4BSD
120 # test numeric conversions -- these four lines should echo identically
121 printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
122 printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
124 printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
125 printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
131 printf "%*d\n" 10 -42
133 # test some simple floating point formats
135 printf "%#4.2f\n" 4.2
136 printf "%#4.1f\n" 4.2
138 printf "%*.*f\n" 4 2 4.2
139 printf "%#*.*f\n" 4 2 4.2
140 printf "%#*.*f\n" 4 1 4.2
152 # test some of the more esoteric features of POSIX.1 printf
153 printf "%d\n" "'string'"
154 printf "%d\n" '"string"'
156 printf "%#o\n" "'string'"
157 printf "%#o\n" '"string"'
159 printf "%#x\n" "'string'"
160 printf "%#X\n" '"string"'
162 printf "%6.2f\n" "'string'"
163 printf "%6.2f\n" '"string"'
165 # output from these two lines had better be the same
166 printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
167 printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
170 printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
171 printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
173 # tests for translating \' to ' and \\ to \
174 # printf translates \' to ' in the format string...
177 # but not when the %b format specification is used
178 printf "%b\n" \\\'abcd\\\'
180 # but both translate \\ to \
182 printf "%b\n" '\\abcd\\'
184 # this was reported as a bug in bash-2.03
185 # these three lines should all echo `26'
192 # this should be an overflow, but error messages vary between systems
193 # printf "%lu\n" 4294967296
195 # ...but we cannot use this because some systems (SunOS4, for example),
196 # happily ignore overflow conditions in strtol(3)
197 #printf "%ld\n" 4294967296
199 # in the future this may mean to put the output into VAR, but for
201 printf -v var "%10d" $RANDOM
206 # this caused an infinite loop in older versions of printf