Sample Header Ad - 728x90

printf seems to fail in bash

0 votes
0 answers
43 views
With version GNU bash, version 5.2.32(1)-release (x86_64-pc-linux-gnu) in present Debian testing this command: $ printf '%010f\n' '1234' 000.000000 Doesn't use the number given and changes on every execution? : $ printf '%010f\n' '1234' 000.000000 $ printf '%010f\n' '1234' -00.000000 $ printf '%010f\n' '1234' -483727882220210383673538789311734784068460135136461336086484727908638813056859257411855537705282271270980907616349527085348099018785353082643221259209294558156190448714309764451049347540471064551446885137304830478611003880772998267972492798437861990242385390167399221730525636524535959100898865048044621829532180872869831407033533680957149104798943111261125290846584672637786950588494241537852501370987086890557439435578999950651169388180476906711478049091875710028706657130770652700584029129393783335159019276229170433952983735319574924819265320406630232536315336832562784656716950007939401275466624481927781927258325705879156330466999432612166518529539015750123768161816142453794507679229993846158691958525270639720288023359751521266391315728694060032591665637983258824799749300768142902211449165371138800414916337009245091261553826987554933951337065582043260565800658802960164297014952829986461975404403328316973961329623423683108873863706900026374773894180497513448143048382250942260806514095625959417231557222342591236652512686787828803609697693656123275076808865225369983011037611625663587582203365010071225681027800095427631778822391073362620068182757407019491657182878003911077240783559336224719331241350462550966676226233143230279692283620645084683412441335955962689659909099953181023376906230488367104.000000 1.- Is this reproducible? Yes, see bug report https://unix.stackexchange.com/questions/783623/bash-printf-float-formatting-became-nonsensical-and-random (thanks steeldriver) 2.- Where is bash reading those values? From uninitialized memory locations as this command reports: $ valgrind bash -c 'printf %f 2' **Solution(s)** 1.-Use Debian stable (which has an older bash version without this bug). 2.- Install a different version of bash. This is the way I did it, I believe that this work, but I can not give any guarantee. Since installing an older version might reproduce older bugs, it is better to install a newer version. The list of bash versions available for general consumption are here: https://ftp.gnu.org/gnu/bash/ Download the bash source and compile it: curl -O https://ftp.gnu.org/gnu/bash/bash-5.2.37.tar.gz tar xvf bash-5.*.tar.gz cd bash-5.2.37 ./configure make And then install it manually: # mv /bin/bash /bin/bash.bak And copy the bash file compiled above to /bin/bash. Reboot to ensure all copies of bash have been flushed from memory. Try: $ LC_ALL=C bash -c "printf '%5.15f\n' '2.1'" 2.100000000000000 And confirm that the version is correct: $ bash --version GNU bash, versión 5.2.37(1)-release (x86_64-pc-linux-gnu)
Asked by smf (1 rep)
Oct 3, 2024, 08:28 PM
Last activity: Oct 7, 2024, 06:25 PM