An arbitrary precision calculator language bc is written in it’s own C-like language Can read from files or stdin like most utilities
running from a file
/* A very simple bc script */
2 + 2
[me@linuxbox ~]$ bc foo.bc
bc 1.06.94
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software
Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
4
bc can be used interactively
[me@linuxbox ~]$ bc -q
2 + 2
4
quit
It’s also possible to pass a script to bc via stdin bc < foo.bc
The ability to accept stdin means that we can use here docs: bc <<< "2+2"
#!/bin/bash
# loan-calc: script to calculate monthly loan payments
PROGNAME="${0##*/}" # Use parameter expansion to get basename
usage () {
cat <<- EOF
Usage: $PROGNAME PRINCIPAL INTEREST MONTHS
Where:
PRINCIPAL is the amount of the loan.
INTEREST is the APR as a number (7% = 0.07).
MONTHS is the length of the loan's term.
EOF
}
if (($# != 3)); then
usage
exit 1
fi
principal=$1
interest=$2
months=$3
bc <<- EOF
scale = 10
i = $interest / 12
p = $principal
n = $months
a = p * ((i * ((1 + i) ^ n)) / (((1 + i) ^ n) - 1))
print a, "\n"
EOF
[me@linuxbox ~]$ loan-calc 135000 0.0775 180
1270.7222490000