Category Archives: Scripts

Calculating Pi (π) with bash

I just created this post for people who ended up in this site searching for ways to get pi value with bash shell. There are multiple ways for calculation but I like following:

1. make sure you have package bc installed. It comes from package named “bc” on debian.
2. run command below where parameter scale is number of decimal places after comma.

export BC_LINE_LENGTH=0;bc -lq <<< "scale=1000;4*a(1)"

Alternatively you could make a shellscript like this:



pi=`bc -lq <<< "scale=${DP};4*a(1)"`

echo ${pi}

exit 0

Environment variable BC_LINE_LENGTH with 0 value is needed to disable result line splitting with backslash and newline.