0

I am trying to create a bash alias or function which can pipe text to the stdin of the unix command bc.

I first attempted:

alias semitone="echo \"scale=10; e(l(2.))\" | bc"

Which bash parses ok, but it fails with a bc runtime error:

Runtime error (func=(main), adr=12): Function l not defined.

In tcsh:

alias semitone 'echo "scale=10; e(l(2.)/12.)" | bc'

works totally fine and gives correct output:

1.0594630943

I also attempted using a bash function:

semitone() { echo "scale=10; e(l(2.)/12.)" | bc ; }                                                                              

which returns the same bc runtime error. Not sure how the output is getting munged. Any insight?

ctpenrose
  • 1,467
  • 2
  • 18
  • 28
  • 2
    That code doesn't work outside a function or alias either. Try just running `echo "scale=10; e(l(2.)/12.)" | bc` at your prompt. – Charles Duffy Mar 06 '14 at 04:45

1 Answers1

3

For math functions to be defined in bc, you need to specify the -l option. Otherwise, bc will tell you that functions like l are not defined.

So it has nothing to do with the alias. I have no idea how it works with tcsh -- it doesn't on my (ubuntu) system. Although including the -l, even through the alias -- as in semitone -l -- works fine both with bash and tcsh

rici
  • 234,347
  • 28
  • 237
  • 341
  • Oops, my mistake. I had bc aliased as bc -l under both tcsh and bash under linux. Been too long for me to remember the function of -l – ctpenrose Mar 06 '14 at 05:08