bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

printf and the $ modifier


From: Maarten Billemont
Subject: printf and the $ modifier
Date: Fri, 19 Dec 2014 09:46:34 -0500

man 3 printf describes the following:

     o   An optional field, consisting of a decimal digit string followed by a $, specifying the next argument to access.  If this field is not provided,
         the argument following the last argument accessed will be used.  Arguments are numbered starting at 1.  If unaccessed arguments in the format
         string are interspersed with ones that are accessed the results will be indeterminate.

This is a useful feature, allowing you to decouple the order of your arguments with from the format.  This is useful for localization and a few other cases.  In this example, I'd like to use it to simplify a regex replace statement:

# s/(.*)foo(.*)/$2bar$1/
[[ 123foo567 =~ (.*)foo(.*) ]] && printf '%2$sbar%1$s' "${BASH_REMATCH[@]:1}"

Is there a particular reason why bash's built-in printf does not support this format modifier?  Does bash re-implement printf or does it use the Standard C Library's printf?  (If the former; why?)

--
Maarten Billemont (lhunath)
me: http://www.lhunath.com – business: http://www.lyndir.com – http://masterpasswordapp.com

reply via email to

[Prev in Thread] Current Thread [Next in Thread]