Creating a calculator script

后端 未结 7 2132
悲哀的现实
悲哀的现实 2020-12-17 06:18

I am trying to make a calculator with a bash script. The user enters a number, chooses whether they wish to add, subtract, multiply or divide. Then the user enters a second

相关标签:
7条回答
  • 2020-12-17 06:51

    This calculates uses up to 4 decimals (if needed), works without quotation marks.

    calc () 
    { 
        echo "scale=4;$*" | bc -l
    }
    

    Only downside is, you have to escape *, else the shell will use it for file expansion.

    Usage:

    calc 1 + 2
    calc 3 - 4
    calc 44 \* 88
    calc 77 / 234
    

    This should do for most cases where you need a calculator fast.

    0 讨论(0)
提交回复
热议问题