Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top