SHELL/BASH: How to create an interactive calculator in Linux

Written by - Deepak Prasad

In this article I will share a sample script which will act as an interactive calculator using shell or bash script. Using this interactive calculator you can perform basic and some advanced mathematical calculations like sine, cosine etc. We will use bc tool to create our interactive calculator.


Sample Interactive Calculator - Shell Script

There will be two parts of our sample script wherein one part will be "scriptbc" which we will add to our PATH variable so that it can be automatically executed instead of using absolute path

[root@node1 ~]# vim /usr/local/bin/scriptbc

# scriptbc--Wrapper for 'bc' that returns the result of a calculation

if [ "$1" = "-p" ] ; then
     shift 2
   precision=2           # Default

bc -q -l << EOF

exit 0

Next we will copy our "scriptbc" tool to /usr/local/bin so that it becomes part of our system binary. We will use this tool to create our interactive calculator.

[root@node1 ~]# cp /tmp/scriptbc /usr/local/bin

Now the last part is to create our main wrapper script for creating our interactive calculator. You can modify the script to use as per your requirement.

[root@node1 ~]# vim /tmp/

# calc--A command line calculator that acts as a frontend to bc


cat << EOF
     In addition to standard math functions, calc also supports:

     a % b       remainder of a/b
     a ^ b       exponential: a raised to the b power
     s(x)        sine of x, x in radians
     c(x)        cosine of x, x in radians
     a(x)        arctangent of x, in radians
     l(x)        natural log of x
     e(x)        exponential log of raising e to the x
     j(n,x)      Bessel function of integer order n of x
     scale N     show N fractional digits (default = 2)

if [ $# -gt 0 ] ; then
   exec scriptbc "$@"

echo "Calc--a simple calculator. Enter 'help' for help, 'quit' to quit."

/bin/echo -n "calc> "

while read command args
  case $command
      quit|exit) exit 0                                  ;;
      help|\?)   show_help                               ;;
      scale)     scale=$args                             ;;
      *)         scriptbc -p $scale "$command" "$args"   ;;


      /bin/echo -n "calc> "

echo ""

exit 0


How it works?

The most interesting part of this code is the while read statement, which creates an infinite loop that displays the calc> prompt until the user exits, either by entering quit or by entering an end-of-file sequence (^D). The simplicity of this script is what makes it extra wonderful: shell scripts don’t need to be complex to be useful!


Verify Interactive Calculator

By default, this script runs as an interactive tool that prompts the user for the desired actions. If invoked with arguments, those arguments are passed along to the scriptbc command instead

[root@kerberos ~]# /tmp/
Calc--a simple calculator. Enter 'help' for help, 'quit' to quit.
calc> 4+5
calc> 4/5
calc> 5/5
calc> 2/8
calc> 7*7
calc> 2^2
calc> 2^7
calc> exit


Floating-point calculations, even those that are easy for us humans, can be tricky on computers. Unfortunately, the bc command can reveal some of these glitches in unexpected ways. For example, in bc, set scale=0 and enter 7 % 3. Now try it with scale=4. This produces .0001, which is clearly incorrect.


Lastly I hope the steps from the article to create an interactive calculator using bash or shell script on Linux was helpful. So, let me know your suggestions and feedback using the comment section.


Wicked Cool Scripts


Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment