title: Bash Scripting and bc
body: <p>I'm trying to write a bash script and I needed to do some floating point math.  Basically I want to do something like this:</p>
  
  <pre><code>NUM=$(echo "scale=25;$1/10" | bc)
  if [ $? -ne 0 ]
  then
  echo bad
  fi
  </code></pre>
  
  <p>The problem I'm running into is $? tends to hold the output from the echo program and not the bc call.  Is there a way I save the output from the bc program into a variable?</p>
  
  <p>EDIT:</p>
  
  <p>Thanks for the quick replies.  Here's another way of looking at the problem.  Say I modified the script a little bit so it looks like this:</p>
  
  <pre><code>#!/bin/bash
  NUM=$(echo "scale=25;$1/10" | bc)
  if [ $? -ne 0 ]
  then
  echo bad
  exit
  fi
  echo "$NUM"
  </code></pre>
  
  <p>When the user inputs a normal floating point value, it works fine:</p>
  
  <pre><code>bash script.sh 1.0
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>.1000000000000000000000000
  </code></pre>
  
  <p>However, when the user enters an incorrect value, the script can't recover:</p>
  
  <pre><code>bash script.sh 1.0a
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>(standard_in) 1: parse error
  </code></pre>
  
  <p>What I'm trying to do is get it to exit gracefully.</p>