Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable line breaks in bc?

Is there a way to disable bc's feature of splitting long numbers into several lines, something like scale variable for controlling decimal places? Sure, I can use sed, but is there a bc way of doing that? Man page at http://www.gnu.org/software/bc/manual/html_mono/bc.html only states that the maximum number of characters per line is 70.

Here's an example of a number being split:

bc -l <<< "scale = 100; a(1) * 4"
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676
like image 549
Vytenis Bivainis Avatar asked Jul 30 '15 19:07

Vytenis Bivainis


1 Answers

The online bc man page doesn't match what I have under "Environment Variables"; the man page for my version (v1.06.95) dated 2006-06-11 claims that you can set BC_LINE_LENGTH=0 to disable line breaks, as a GNU-specific extension:

BC_LINE_LENGTH

This should be an integer specifying the number of characters in an output line for numbers. This includes the backslash and newline characters for long numbers. As an extension, the value of zero disables the multi-line feature. Any other value of this variable that is less than 3 sets the line length to 70.

You can set BC_LINE_LENGTH as an environment variable in your shell, or inline preceding the call to bc:

BC_LINE_LENGTH=0 bc -l <<< "scale = 100; a(1) * 4"

Epilogue: After some sleuthing, it seems that though gnu.org's latest version is v1.06 dated 2000-11-15 and matching the documentation published above, there are new upstream versions (v1.06.94 and v1.06.95) hosted on alpha.gnu.org. These newer versions contain the BC_LINE_LENGTH=0 feature.

See the Debian-specific bug report and patch created in 2004 and published in 2007 (!) that adds this extension, as well as Debian's bc changelog. It appears this should be available in modern versions of Debian and Ubuntu, but may not appear for other distributions.

In older implementations, picking an arbitrary large number (BC_LINE_LENGTH=5000) will likely get you where you want to go.

like image 93
Jeff Bowman Avatar answered Oct 30 '22 13:10

Jeff Bowman