Path: csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Robert Elz Newsgroups: gnu.bash.bug Subject: Re: %q with truncating size loses safeness of %q Date: Sat, 18 Apr 2020 05:37:52 +0700 Lines: 35 Approved: bug-bash@gnu.org Message-ID: References: <4bacf2f0-9802-67d3-f30b-80e37d058a4a@case.edu> <22039.1587163072@jinx.noi.kre.to> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1587163115 21365 209.51.188.17 (17 Apr 2020 22:38:35 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org, Sam Liddicott To: chet.ramey@case.edu Envelope-to: bug-bash@gnu.org In-Reply-To: <4bacf2f0-9802-67d3-f30b-80e37d058a4a@case.edu> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 2001:3c8:9009:181::2 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <22039.1587163072@jinx.noi.kre.to> X-Mailman-Original-References: <4bacf2f0-9802-67d3-f30b-80e37d058a4a@case.edu> Xref: csiph.com gnu.bash.bug:16162 Date: Fri, 17 Apr 2020 16:12:20 -0400 From: Chet Ramey Message-ID: <4bacf2f0-9802-67d3-f30b-80e37d058a4a@case.edu> | I would say this is a programmer error. The way precisions work with | string arguments is that the argument is fetched or generated (this | includes generating the quoted string for %q or the expanded string for | %b) and then printf writes number of bytes (!) from that generated string | specified by the precision. This happens only because of the cheap way we (and I presume you) implement things - in any rational scheme, it would take the precision chars from the source string, and then quote them. But that's hard - instead we just use printf to do the work, %q quotes the arg string, and then the 'q' is changed to a 's' in the format, and we just call printf(3) to do the work (padding, justification, ...) The only excuse for this is pragmatics, no-one would deliberately set out to design something quite that bogus. The end result is as Greg said, "Don't do that", if precisions are needed with %q do something like printf 'echo %q%q\n' "$(printf %.2s "a'b")" ';ls' which produces echo a\'\;ls which I expect is the desired result. kre