Correction after off-list poke: FreeBSD's /usr/bin/printf and the printf builtin to /bin/sh both show "x61"
On Thu, Apr 15, 2021 at 7:04 PM Philip Guenther <guent...@gmail.com> wrote: > The general question is what requirements the standard put on the printf > utility when the format argument contains a \x or other unspecified > backslash escape, but the example in the subject is a nice concrete > example: what's required for or about the output of > printf '\x61' > ? > > 1003.1-2016 describes the handling of the format argument like this: > ----- > The format operand shall be used as the format string described in > XBD Chapter 5 (on page 121) with the following exceptions: > ----- > > ...followed by a list of 11 exceptions that do not cover \x. So, > let's look at XBD Chapter 5: > ----- > The format is a character string that contains three types of objects > defined below: > 1. Characters that are not "escape sequences" or "conversion > specifications", as described below, shall be copied to the output. > > 2. Escape Sequences represent non-graphic characters and the > escape character (<backslash>). > > 3. Conversion Specifications specify the output format of each > argument; see below. > ----- > > Okay, so if it's not an escape sequence or conversion specification, it > _shall_ be copied to the output. To jump forward to conversion > specifications: > ----- > Each conversion specification is introduced by the <percent-sign> > character ('%'). > ----- > > Okay, so \x61 isn't a conversion specification. Is it an escape > sequence? Well, there's just a table for those, which lists the following: > \\ \a \b \f \n \r \t and \v. There's no "other sequences starting with > <backslash> are unspecified" statement that I can find. > > It therefore appears to me that > printf '\x61' > > is required by the standard to output > \x61 > > without a following newline. Unfortunately, the systems I've > tested (CentOS 6 and 7, MacOS, FreeBSD 12, and OpenBSD 6.9) all output an > ascii 'a' without a following newline. > > Did I miss a statement about <backslash> somewhere that renders this > behavior unspecified? > > > If a wording tweak is deemed to be in order, please note that it should be > placed or duplicated such that it also applies to the argument interpreted > by the %b format conversion, because the same "apparently specified but no > one behaves that way" is true of this: > printf %b '\x61' > > > Philip Guenther >