Score:0

How to create calculator with bash?

us flag

How to create calculator with bash ?

example

    read -p "calculator count: "  calc
    echo '$((calc))' | bc

i input

2+2

output

(standard_in) 1: illegal character: $

how to fix ?

I just want to make text input in the script, so for the addition of + , - , x - and / . I input manually

for example I want 2+2+1*3/2

After I enter, the result will appear

Terrance avatar
id flag
Get rid of the single quotes since it makes it literal. Use double quotes or none for `echo '$((calc))'`. So it should be `echo "$((calc))"` or `echo $((calc))`
Joe Cola avatar
us flag
thank you, work
Score:4
ar flag

As already pointed out by Terrance, you need double quotes; otherwise, the $ sign is sent literally.

Also, remove the double parentheses which ask Bash to do the calculation. In which case you wouldn't need to pipe to bc, but Bash only does integer arithmetic.

So it should be either

echo "$((calc))" # evaluated by Bash

or

echo "$calc" | bc
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.