Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
try setting BC_LINE_LENGTH like Stephane does in this answer: unix.stackexchange.com/a/176966/70524 –  muru 2 days ago
4  
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 2 days ago

5 Answers 5

up vote 11 down vote accepted
echo "scale=2; 2^500" | bc | tr -d '\n\\'

Output:

3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376
share|improve this answer
    
I though that tr works only with one char at a time ? oO –  sputnick 2 days ago

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
share|improve this answer
    
Thanks for the edit @Stéphane Chazelas :) –  fredtantini 2 days ago
    
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. –  J. C. Leitão 2 days ago

Thinking outside the box:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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