Bash: find out PI

A long time ago I wrote a script to find out the numbers behind the number PI but that was written in VBS. And today I wanted to recreate that script but than in Bash. I succeeded completely and here is the code:

 1#!/bin/bash
 2x=4;
 3endNr=$x;
 4lastUsed="+";
 5counter=3;
 6while [[ $counter -lt 1000000 ]];
 7do
 8    if [[ $lastUsed == "+" ]];
 9    then
10        newNr=$(echo "scale=30; $x/$counter" | bc);
11        endNr=$(echo "scale=30; $endNr-$newNr" | bc);
12        lastUsed="-";
13    else
14        newNr=$(echo "scale=30; $x/$counter" | bc);
15        endNr=$(echo "scale=30; $endNr+$newNr" | bc);
16        lastUsed="+";
17    fi;
18    counter=$[$counter+2];
19    echo $endNr;
20done;

Usage (Linux):

Step 1: Put the code inside a file and save it as a .sh file.
Step 2: In command line give the file a chmod of +x (eg. chmod +x fileName.sh)
Step 3: Than run the file (eg. ./fileName.sh)

And here you go.

Have fun!!

© 2026 Jurjen