printf command inside a script returns “invalid number”

前端 未结 3 1951
生来不讨喜
生来不讨喜 2020-12-10 02:37

I can\'t get printf to print a variable with the %e descriptor in a bash script. It would just say

#!/bin/bash
a=14.9
printf %e 14.9;

相关标签:
3条回答
  • 2020-12-10 02:50

    This:

    LC_NUMERIC="en_US.UTF-8" printf %e 14.9
    

    sets $LC_NUMERIC only for the duration of that one command.

    This:

    export LC_NUMERIC="en_US.UTF-8"
    

    sets $LC_NUMERIC only for the duration of the current shell process.

    If you add

    export LC_NUMERIC="en_US.UTF-8"
    

    to your $HOME/.bashrc or $HOME/.bash_profile, it will set $LC_NUMERIC for all bash shells you launch.

    Look for existing code that sets $LC_NUMERIC in your .bashrc or other shell startup files.

    0 讨论(0)
  • 2020-12-10 03:05

    I bumped into this error and found this page. In my case, it was 100% pilot error.

    month=2
    printf "%02d" month
    

    it should be

    printf "%02d" "${month}"
    

    or more simply

    printf "%02d" $month
    
    0 讨论(0)
  • 2020-12-10 03:13

    You could have a locale problem, and it wasn't expecting a period. Try:

    LC_NUMERIC="en_US.UTF-8" printf %e 14.9
    
    0 讨论(0)
提交回复
热议问题