19

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
Eric Carvalho
  • 55,573
shaolin
  • 303

2 Answers2

31

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")
  • 1
    Yes I'm using bash, can you please clarify on the subshell? Is there a disadvantage? – shaolin Dec 15 '12 at 22:16
  • 2
    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 'SO- stop being evil' 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
  • @gniourf, thanks for your info!! really..it was helpful – shaolin Dec 15 '12 at 22:54
  • See a discussion with benchmarks on [unix.se]: echo vs <<<, or Useless Use of echo in Bash Award? It seems that if you care about performance, you should use a heredoc, not a herestring, and most importantly you should use dash or ksh. Using <<< in bash is a waste of resources (but not an important one). – Gilles 'SO- stop being evil' Dec 21 '12 at 01:42
  • By the way, you can get the output of bc with var3=$(echo … | bc). It's in $?, same as with var3=$(bc <<<…). – Gilles 'SO- stop being evil' Dec 21 '12 at 01:43
  • Stephane Chazelas explains why <<< is slow: it creates a temporary file, which is slower than a pipe. The data has to come from somewhere after all. – Gilles 'SO- stop being evil' Dec 21 '12 at 02:37
  • 1
    @Gilles just compare time for i in {1..10000}; do : <<< ""; done with time for i in {1..10000}; do echo "" | : ; done, you'll see a dramatic difference. Then you're right, a heredoc is slightly faster than a herestring. But it's more awkward to type as a one-liner. – gniourf_gniourf Dec 21 '12 at 07:57
  • @gniourf_gniourf Now try it with the actual command here instead of an overly simplified example (which doesn't run an external command and doesn't read the data). With bash, and /tmp on tmpfs, I don't see any difference between echo … | and <<<. With ksh, <<< is indeed measurably faster (and faster than bash). – Gilles 'SO- stop being evil' Dec 21 '12 at 10:17
  • @Gilles ok, then continue to use echo | .... I really don't care. – gniourf_gniourf Dec 21 '12 at 12:11
  • @Gilles with <<<, it's not just about slowness! It's also always a problem on read-only environments, because on more complex scripts, your screen will be flooded with ugly error messages. And here's another thing people sometimes have difficulties to understand: this may happen __even under root! Because even root can't write everywhere, if partitions are mounted read-only. Very common on other companies' systems that you only have restricted access to. Likewise, recovery consoles would show that behavior by default as well. – syntaxerror Oct 24 '14 at 00:27
11

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

var3=$(echo "scale=2;$var1/$var2" | bc)
  • 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