title: minimal typing command line calculator - tcsh vs bash
body: <p>I like to have a command-line calculator handy.  The requirements are:</p>
  
  <ul>
  <li>Support all the basic arithmetic operators: +, -, /, *, ^ for exponentiation, plus parentheses for grouping.</li>
  <li>Require minimal typing, I don't want to have to call a program interact with it then asking it to exit.</li>
  <li>Ideally only one character and a space in addition to the expression itself should be entered into the command line.</li>
  <li>It should know how to ignore commas and dollar (or other currency symbols)
  in numbers to allow me to copy/paste from the web without worrying
  about having to clean every number before pasting it into the calculator</li>
  <li>Be white-space tolerant, presence or lack of spaces shouldn't cause errors</li>
  <li>No need for quoting anything in the expression to protect it from the shell - again for the benefit of minimal typing</li>
  </ul>
  
  <p>Since tcsh supports alias positional arguments, and since alias expansion precedes all other expansions except history-expansion, it was straight forward to implement something close to my ideal in tcsh.</p>
  
  <p>I used this:</p>
  
  <pre><code>alias C 'echo '\''\!*'\'' |tr -d '\'',\042-\047'\'' |bc -l'
  </code></pre>
  
  <p>Now I can do stuff like the following with minimal typing:</p>
  
  <pre><code># the basic stuff:
  tcsh&gt;  C 1+2
  3
  
  # dollar signs, multiplication, exponentiation:
  tcsh&gt;  C $8 * 1.07^10
  15.73721085831652257992
  
  # parentheses, mixed spacing, zero power:
  tcsh&gt;  C ( 2+5 ) / 8 * 2^0
  .87500000000000000000
  
  # commas in numbers, no problem here either:
  tcsh&gt;  C 1,250.21 * 1.5
  1875.315
  </code></pre>
  
  <p>As you can see there's no need to quote anything to make all these work.</p>
  
  <p>Now comes the problem.  Trying to do the same in bash, where parameter aliases aren't supported forces me to implement the calculator as a shell function and pass the parameters using "$@"</p>
  
  <pre><code>function C () { echo  "$@" | tr -d ', \042-\047' | bc -l; }
  </code></pre>
  
  <p>This breaks in various ways e.g:</p>
  
  <pre><code># works:
  bash$  C 1+2
  3
  
  # works:
  bash$  C 1*2
  2
  
  # Spaces around '*' lead to file expansion with everything falling apart:
  bash$  C  1 * 2
  (standard_in) 1: syntax error
  (standard_in) 1: illegal character: P
  (standard_in) 1: illegal character: S
  (standard_in) 1: syntax error
  ...
  
  # Non-leading parentheses seem to work:
  bash$  C  2*(2+1)
  6
  
  # but leading-parentheses don't:
  bash$  C  (2+1)*2
  bash: syntax error near unexpected token `2+1'
  </code></pre>
  
  <p>Of course, adding quotes around the expression solves these issues, but is against the original requirements.</p>
  
  <p>I understand <em>why</em> things break in bash. I'm not looking for explanations.  Rather, I'm looking for a <em>solution</em> which doesn't require manually quoting the arguments.  My question to bash wizards is is there any way to make bash support the handy minimal typing calculator alias. Not requiring quoting, like tcsh does?  Is this impossible?  Thanks!</p>