7

I want to store the value of 2^500 in the variable DELTA.

I'm doing

export DELTA=$(echo "scale=2; 2^500" | bc)

but this does not set DELTA to 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376. Instead, it sets it to

32733906078961418700131896968275991522166420460430647894832913680961\
33796404674554883270092325904157150886684127560071009217256545885393\
053328527589376

I tried the answers in this question (3 years old), using

export DELTA=$(echo "scale=2; 2^500" | bc | tr '\n' ' ') 

or

export DELTA=$(echo "scale=2; print 2^500" | bc | tr '\n' ' ')

but none of them work for setting the variable, only to echo it. Any idea?

  • try setting BC_LINE_LENGTH like Stephane does in this answer: unix.stackexchange.com/a/176966/70524 – muru Jan 7 '15 at 15:12
  • 5
    You already got the answer(s), I only want to add that you don't need echo command. Instead use Here String redirection: bc <<< "scale=2; 2^500" – jimmij Jan 7 '15 at 17:08
  • @jimmij One thing; going the echo way is much more portable (think commercial *nixes). Plus, you must ensure that your /tmp is writable if you use here strings. Think of emergency consoles with / mounted ro - your here string approach will FAIL. – syntaxerror Apr 17 '15 at 19:12
12
echo "scale=2; 2^500" | bc | tr -d '\n\\'

Output:

3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376
  • 1
    I though that tr works only with one char at a time ? oO – Gilles Quenot Jan 7 '15 at 15:20
  • @StardustOne Not really. You can use arbitrary groups of characters with the two parameters, e. g. try echo "havk vf sha" | tr 'fhaskv' 'sunfxi' :) (this is rot13 for the poor man, with the chars deliberately unsorted to not give away the solution too soon :)) – syntaxerror Apr 17 '15 at 19:25
14

In the GNU implementation of bc, there is the environment variable BC_LINE_LENGTH (cf man bc):

~$ echo "scale=2; 2^500" | bc
32733906078961418700131896968275991522166420460430647894832913680961\
33796404674554883270092325904157150886684127560071009217256545885393\
053328527589376
~$ export BC_LINE_LENGTH=99999 #or better 0
~$ echo "scale=2; 2^500" | bc
3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376
  • 1
    I like this solution most, except that BC_LINE_LENGTH=0 does not work in my machine (Mac OS X), and I'm using really big numbers, so changing BC_LINE_LENGTH depending on the number is not enough for me. +1 in any case. – Jorge Leitao Jan 7 '15 at 15:29
3

Thinking outside the box:

export DELTA=$(python -c 'print(2**500)')
2
> echo "scale=2; 2^500" | bc | { read value; echo "$value";}
3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376
1
$ echo "scale=2; 2^500" | bc | perl -pe 's/\\\n//'
3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.