Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input in Bash.
Enter two numbers: 5.6 3.4 Enter Choice: 1. Addition 2. Subtraction 3. Multiplication 4. Division 3 5.6 * 3.4 = 19.0
1. Read Two Numbers 2. Input Choice (1-Addition, 2-Subtraction, 3-Multiplication, 4-Division) 3. if Choice equals 1 Calculate res = a + b else If Choice equals 2 Calculate res = a - b else if Choice equals 3 Calculate res = a * b else if Choice equals 4 Calculate res = a / b 4. Output Result, res
echo is one of the mostly used command.
It is used to print a line of text in standard output.
$ echo [-neE] [arg ...]
The command read in the Linux is used to read the input from the keyboard.
When there are a lot of if statement in Shell and it becomes confusing. Then it is good to use case statement.
4. bc Command
Checkout the link for bc Command bc Command Linux Example
- Average of given numbers in Bash
- Fibonacci Series in Bash
- Programs for printing different patterns in Bash
- Bash program to find A to the power B
- Bash shell script to swap two numbers
- Useful and time saving bash commands in Linux
- Bash Script to get Low Battery Alert in Linux
- Bash program to check if the Number is a Palindrome
- Bash shell script to find sum of digits
- Bash program to check if the Number is a Prime or not
- Sorting an array in Bash using Bubble sort
- Write a bash script to print a particular line from a file
- How to start nmap and run a simple scan ?
- What is Linux System Administration?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.