Write the command to perform all the arithmetic operations using basic calculator. In unix
Answers
Answer:
Arithmetic Operations / Expressions in Unix / Linux Bash Script
Unix or linux operating systems provides the bc and expr commands for doing arithmetic calculations. In this article, we will see how to do arithmetic operations in the unix shell script. I recommend the following articles before going to use the bc and expr commands in the bash script:
bc command tutorial
expr command tutorial
The following basic examples shows how to do arithmetic calculations in the shell scripts using the bc and expr command:
1. Sum of numbers
#!/bin/bash
#assigning values to the variables
a=9
b=7
res=`expr $a + $b`
echo $res
res=`echo "$a+$b" | bc`
echo $res
#using the let command to find the sum
let z=$a+$b
echo $z
#using braces
echo $(($a+$b))
2. Difference between two numbers
#!/bin/bash
#assign values to the variables
a=10
b=7
res=`expr $a - $b`
echo $res
res=`echo "$a-$b" | bc`
echo $res
3. Multiplication and division
#!/bin/bash
#assign values to the variables
a=2
b=3
res=`expr $a * $b`
echo $res
res=`echo "$a*$b" | bc`
echo $res
4. Length of the string
#!/bin/bash
str="linux dedicated server"
len=`expr length "$str"`
echo $len
5. Printing numbers from 1 to 10 using for loop
#!/bin/bash
echo 'for(start=1;start <= 10;start++) {start;}' | bc
Explanation:
Answer:
bc
Explanation: