Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing a script and I would like to pass the results from bc into a variable. I've declared 2 variables (var1 and var2) and have given them values. In my script I want to pass the results from bc into another variable say var3 so that I can work with var3 for other calculations. So far I have been able write the result to a file which is not what I'm looking for and also I've been able to echo the result in the terminal but I just want to pass the result to a variable at moment so that I can work with that variable.

echo "scale=2;$var1/var2" | bc
share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you're using bash, you'd better use an here string instead of a pipe as in:

bc <<< "scale=2;$var1/$var2"

This will save you a subshell.

Then, to store the output of a command, use a command substitution:

answer=$(bc <<< "scale=2;$var1/$var2")

Edit.

If you want something even cooler than bc, here's dc (reverse polish calculator):

answer=$(dc <<< "2k $var1 $var2/p")
share|improve this answer
1  
Yes I'm using bash, can you please clarify on the subshell? Is there a disadvantage? –  shaolin Dec 15 '12 at 22:16
1  
Each time you use a pipe | the command on the right of the pipe is executed in a subshell, and it takes resources to open a new subshell (it's like opening a new instance of bash to execute that command). If you can avoid it, avoid it. Here, piping an echo to bc will run the command bc in a subshell and is, in some sense, retarded (no offence), since bash has the wonderful here-string construct <<< to avoid stupid things like these. –  gniourf_gniourf Dec 15 '12 at 22:19
    
@gniourf_gniourf Then again, if you cared about resource usage, you'd use /bin/sh (dash) instead of bash, but dash doesn't have <<<. –  Gilles Dec 15 '12 at 22:33
    
Thank you for the helpful information!! This is good information to know regarding preserving resources and subshells. I greatly appreciate you input. –  shaolin Dec 15 '12 at 22:37
    
@Gilles please don't distort the discussion, you know I'm talking about useless and avoidable wastages of resource. Because going further in your direction, if I cared about resources I would not have a computer at all. Please let's be serious, so let me ask you a serious question: how can you (easily) check for the return code of bc if it's in a subshell like in your answer? –  gniourf_gniourf Dec 15 '12 at 22:45

Command substitution stores the output of a command into a variable.

var3=$(echo "scale=2;$var1/$var2" | bc)
share|improve this answer
    
Thank you! This got me exactly where I wanted to be, after probably an hour of researching. Thanks –  shaolin Dec 15 '12 at 22:18

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.