0

I have a large number and I want to make bc calculation. Example:

T_Mab = 6.00899e+09

and I wanna print it like this:

echo 'T_Mab =   '${T_Mab}' [s] = '${T_Mab}/31557600' [year]' | bc -l

It is giving me "syntax error". So how can I do it?

  • 1
    The example looks incomplete - `${T_Mab_y}` undefined. You should be able to simplify it a lot more! E.g. `bc -l <<<"6.00899e+09"` (which gives me a syntax error - I don't think that `bc` reads "e" notation). – Toby Speight Nov 22 '18 at 15:04
  • Did you set it like you show, with spaces around `=`? There can't be any. – Benjamin W. Nov 22 '18 at 15:05
  • Also, try your command in an interactive bc session. Variable names can't contain uppercase characters – Benjamin W. Nov 22 '18 at 15:06
  • 1
    And thirdly, this'll just assign your result to a `bc` variable, which will promptly disappear afterwards. Did you mean something like `T_Mab=$(echo '...' | bc -l)`, perhaps? – Benjamin W. Nov 22 '18 at 15:06
  • Your expression reads `something = something = something`, why are there two equals signs? – Benjamin W. Nov 22 '18 at 15:08
  • 1
    Possible duplicate of [How to get bc to handle numbers in scientific (aka exponential) notation?](https://stackoverflow.com/questions/12882611/how-to-get-bc-to-handle-numbers-in-scientific-aka-exponential-notation) – Toby Speight Nov 22 '18 at 17:01

2 Answers2

1

You cannot just write whatever you want to display and dump them to bc. Another issue is that bc does not accept scientific notations. Check [How to get bc to handle numbers in scientific (aka exponential) notation? for details.

Assuming that the number is already converted to the correct form as in the answers in the linked question, you can write it like this in bash.

T_Mab=6008990000
echo "${T_Mab} [s] = $(bc -l <<< ${T_Mab}/31557600) [year]"

Here-strings are added since bash 3.0, if you are using older version, just use $(echo ${T_Mab}/31557600|bc -l).

With all these said, you really should consider bc alternatives as suggested in the second answer of the linked question if you don't need arbitrary precision.

Weijun Zhou
  • 746
  • 1
  • 7
  • 25
  • Perhaps I should had written my script completely... The problem is I have a file 10 columns, the first one is the time in seconds in the same notation from the number I used as example. The second column is the ID. So I need to find the average time from a defined ID's – Patrícia Buzzatto Siqueira Nov 22 '18 at 15:59
  • You need to put all these information, along with sample input, the current script you are using, as well as expected output, into your question to clarify what you are trying to achieve. Don't put these in comments. – Weijun Zhou Nov 22 '18 at 16:54
0

The syntax error is because bc doesn't read "e" notation, and can be reproduced with a greatly simplified example:

$ bc -l <<<"6.00899e+09"
(standard_in) 1: syntax error

We'll need to change to a syntax it does understand; we can do that in Bash:

v=6.00899e+09
v=${v/e/*10^}    # 6.00899*10^+09
v=${v/^+/^}      # 6.00899*10^09
bc -l <<<"($v)"
6008990000.00000

Or simply launder through a tool that does understand the notation:

printf '%f\n' "$v" | bc -l
Toby Speight
  • 27,591
  • 48
  • 66
  • 103