2023-09-01 07:13:36 +0100, Stephane Chazelas via austin-group-l at The Open Group: > 2023-08-31 10:35:59 -0500, Eric Blake via austin-group-l at The Open Group: > > In today's Austin Group call, we discussed the fact that printf(1) has > > mandated behavior for %b (escape sequence processing similar to XSI > > echo) that will eventually conflict with C2x's desire to introduce %b > > to printf(3) (to produce 0b000... binary literals). > [...] > > Is C2x's %b already set in stone? > > ksh93's printf (and I'd expect ast's standalone printf) has > %<width>[,<precision>[,<base>]d to output a number in an > arbitrary base which IMO seems like a better approach than > introducing a new specifier for every base. [...]
For completeness, several shells also support expanding integers in arbitrary bases. Like ksh's typeset -i2 binary=123 already there in ksh85, possibly earlier, also available in pdksh and derivatives and zsh. Originally with the base number not specified the output base was derived from the first assignment like typeset -i var; var='2#111' would get you a $var that expands in binary. Looks like that was discontinued in ksh93, but it's still there in mksh or zsh. And there's also: $ echo $(( [#2] 16 )) $(( [##2] 16 )) 2#10000 10000 In zsh (note that you don't get 0b10000 upon $(( [#2] 16 )) after set -o cbases). If bash added: printf -v var %..2 16 à la ksh93, that would bridge that gap. How to output/expand numbers in bases other thn 8, 10, 16 is a recurring question for bash, with people generally surprised that it can *input* numbers in any base, but not *output* in any base. See https://unix.stackexchange.com/questions/415077/how-to-add-two-hexadecimal-numbers-in-a-bash-script/415107#415107 https://unix.stackexchange.com/questions/616215/bash-arithmetic-outputs-result-in-decimal https://unix.stackexchange.com/questions/749988/arbitrary-base-conversion-from-base-10-using-only-builtins-in-bash to list only a few. -- Stephane