: ' Changes:
Sat May 16 2026 Added option to start with formula i.e. calc 5+9
Sat May 16 2026 Cleaning up formating and comments
Sat May 16 2026 First upload
'
[ "$1" == "-D" ] && DEBUG=1 && shift 1
[ "$DEBUG" == 1 ] && echo "---Variables---" && set -x
PS4='$SECONDS $LINENO: '
DOW=$(date +%a)
TODAY=$(date +%m/%d)
DOM=$(date +%d)
OS=$(uname -s)
NAME=${0##*/}
DIR="$(basename $PWD)"
PDIR="$(cd ..;basename $PWD)"
HISTORY_FILE="$HOME/.calculator_tape.txt"
DECIMALS=2
R=$(tput setaf 1)
BR=$(tput setaf 1; tput bold)
G=$(tput setaf 2)
BG=$(tput setaf 2; tput bold)
Y=$(tput setaf 3)
BY=$(tput setaf 3; tput bold)
B=$(tput setaf 4)
BM=$(tput setaf 5; tput bold)
BC=$(tput setaf 6; tput bold)
C=$(tput setaf 6)
BL=$(tput setaf 7; tput bold)
BLD=$(tput bold)
N=$(tput sgr0)
SIT=$(tput sitm)
RIT=$(tput ritm)
UL=$(tput smul)
NL=$(tput rmul)
RV=$(tput rev)
ROWS=$(tput lines)
COLS=$(tput cols)
[ "$DEBUG" == 1 ] && set +x && read -p "${N}-Continue-" -n 1 x
bl(){
echo ""
}
html(){
vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}
log(){
[ "$DEBUG" == "1" ] && set -x
logger -i -t "$NAME" "$*"
}
pause(){
[ "$DEBUG" == "1" ] && set -x
[ "$1" == "-nt" ] && TMOUT="" && shift
echo "$BY";
if [ $# -gt 0 ]
then
read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
else
read -r -p "${C}Hit any key${N}" -n 1 FOO;
fi;
bl
}
strip-tags(){
sed -e 's/<[^>]*.//g' -
}
xtitle(){
printf "\033]0;%s\007" "$*"
}
if [ "$1" == "-E" ]
then
vim $0
sed -i -e "7s/.*/# Updated....: $(date)/" $0
log "Updated $0"
html $0
cp $0 /var/www/unix
mv $0.html /var/www/unix
exit
fi
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
$NAME
No options
END
exit
fi
command -v bash >/dev/null || sudo apt install bash -qyy
command -v rlwrap >/dev/null || sudo apt install rlwrap -qyy
if [[ "$1" == "-v" || "$1" == "--version" ]]
then
grep -E '^# Updated' $0
bl
sed -n "/' Changes/,/^ *$/p" <$0 |head -n 7
read -p "View source (y|n)? " -n 1 ANS
set -x
[ "$ANS" == "y" ] && bat $0
exit 0
fi
touch "$HISTORY_FILE"
[ "$DEBUG" == 1 ] && echo "---Functions---" && typeset -F && read -p "${N}-Continue-" -n 1 x
[ "$DEBUG" == 1 ] && echo "---Main---" && set -x
if [[ "$#" = 0 ]]
then
echo "${BG}===${N}${BLD}Bash Calculator with History Tape${N}${BG}===${N}"
echo "Type your math equation (e.g., 22 / 7) or ${SIT}'history'${RIT} to view the tape."
echo "${BR}q${BC} to exit. ${BLD}Enter will clear last result in stack.${N}"
fi
while true; do
if [[ $# = 0 ]]
then
input=$(rlwrap -S '> ' -H HISTORY -P "$a" -o cat)
else
input="$@"
shift "$#"
fi
if [[ "$input" == "history" || "$input" == "h" ]]; then
echo "--- History Tape ---${BC}"
cat "$HISTORY_FILE"
echo "${N}--------------------"
continue
fi
if [[ -z "$input" ]]; then
continue
fi
if [[ "$input" = "$a" ]]; then
a=""
continue
fi
[[ "$input" = "q" ]] && exit
result=$(echo "scale=$DECIMALS; $input" | bc -l 2>/dev/null)
a=$result
if [[ -z "$result" ]]; then
echo "${BR}Error:${BY}${SIT} Invalid mathematical expression.${RIT}${N}"
else
echo "= ${BG}$result${N}"
echo "$input = $result" >> "$HISTORY_FILE"
fi
done
exit