Calculating Pi (π) with bash

I just created this page 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:

#!/bin/bash

DP=1000
export BC_LINE_LENGTH=0

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.

Check out also how to do other calculations in bash

If you found this useful, say thanks, click on some banners or donate, I can always use some beer money.