Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like

echo 'scale=25;65320/670' | bc

but it would be great if I could define a .bashrc function that looked like

divide () {
  bc -d $1 / $2

I have a handy bash function called calc:

calc () {
    bc -l <<< "$@"

Example usage:

$ calc 65320/670

$ calc 65320*670

You can change this to suit yourself. For example:

divide() {
    bc -l <<< "$1/$2"

Note: <<< is a here string which is fed into the stdin of bc. You don't need to invoke echo.

|improve this answer|||||
  • 3
    I agree it's looking better without the additional echo thrown in, but since it's a shell builtin, it comes with little to no additional cost. What keeps me from using '<<<' in shell functions is its limitation to bash, while the additional echo allows using it in the original bourne shell or ksh without changes. – Tatjana Heuser Jan 31 '12 at 20:49
  • @TatjanaHeuser ksh, the 93-variant, supports here-strings. – Kusalananda Apr 19 '17 at 16:05
  • To assign to a variable, I did this: foo=$(bc -l <<< "( 2 * 5 ) + 5") – thebiggestlebowski Feb 12 '18 at 13:51

Bash can do the math itself to some extent. It's not useful for accuracy, though, as it rounds.

[user]$ echo $(( 10/5 ))

But you're exactly right - a bash function would be a simple shortcut and your example basically works.

divide() {
  echo "scale=25;$1/$2" | bc

Throw that in your .bashrc and then you can:

[user]$ divide 10 5
|improve this answer|||||
  • 1
    echo $((10 / 5)) needs no quotation. – user unknown Jan 31 '12 at 23:15
  • 1
    Significant annoyance: echo $((6383/7671)) will give you zero. You need to be explicit about floating point numbers: echo $((6383.0/7671.0)) – Sridhar Sarnobat Jan 29 '16 at 19:39

You probably know the bash builtin 'expr' as in

$ expr 60 / 5

which is limited to integers and needs the spaces between the arguments.

What is keeping you from defining a function along the lines of the echo expression you're already using? I.e.

 divide () {
   echo $1/$2 | bc
|improve this answer|||||
  • 1
    just ignorance. i didn't realize i could make it work that way... – ixtmixilix Feb 1 '12 at 20:46

Not really an answer to this precise question, but it might be good to know. Use zsh ;-)

% echo $((65320./670))
|improve this answer|||||
  • I was actually looking for this question because I changed to zsh and couldn't use bash's expr builtin, so good answer regardless :) – Steen Schütt Aug 16 '17 at 17:12

If you have calc installed on your system and you don't like rounding, you can:

div() { calc "$1 / $2"; }
|improve this answer|||||
  • hmm... couldn't find any debian package for 'calc' – ixtmixilix Jan 31 '12 at 17:12
  • The package is called apcalc (Arbitary Precision)... The binary is called calc – Peter.O Jan 31 '12 at 17:37
  • @Peter.O Ok, in the Arch Linux repos it's called calc. – user13742 Jan 31 '12 at 18:06

A dirty hack for small values and a limited precision without using bc would be, to multiply the nominator before division, to get an accurate result.

Example without precision:

echo $((13/7)) 

and with 2 digits precision: multiply by 100 and move the decimal point 2 steps to the left:

echo $((100*13/7)) | sed 's/..$/.&/'
echo $((100*13/7))%

This is only useful if the range of numbers is known before, and the precision is always the same. Avoiding to call bc, and calling sed seems not very reasonable.

Note, that multiplying the values might lead to overflow errors, but not very early:

echo $((1000000000*12345678901))
|improve this answer|||||
  • Using double parentheses to evaluate math expressions is deprecated. Use: $[expression] instead – laebshade Feb 6 '12 at 23:06
  • 3
    @laebshade: I can prove you wrong, the opposite is true. Here is a citation of the bash manual: The old format $[expression] is deprecated and will be removed in upcoming versions of bash. – user unknown Feb 7 '12 at 21:29
  • Also discussed here, here and here. – G-Man Says 'Reinstate Monica' Jun 28 '19 at 19:13

Instead of using bc, you could use awk:

div() { awk -v p=$1 -v q=$2 'BEGIN{print p/q}'; }
|improve this answer|||||
echo Division
echo $num1 "/" $num2 "=" $efgh.$remainder
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.