The cookie is used to store the user consent for the cookies in the category "Analytics". This cookie is set by GDPR Cookie Consent plugin. These cookies ensure basic functionalities and security features of the website, anonymously. Necessary cookies are absolutely essential for the website to function properly. List Of Tutorials available in this website: C Programming 20 Chaptersĭata Structures and Algorithms 85 Chapters You can enter “man bc” in shell to get to know default math operations that a shell has to offer. To specify that library we use “-l” after “bc”. To perform complex operations like finding a square of a number or square root of a number, we need to specify math library. For example, we need decimal place up to 3 numbers then we can specify “scale=3 ” Example: #!/bin/bash For division we need to append with “scale” keyword followed by number of decimal places needed. Note: “bc” tool will work correctly for all the operations, except for division. We give the input to “bc” with the help of pipe “|” as shown below. To perform operations on floating point we need to use “bc” tool. #!/bin/bashĠ 8.2 Performing operations on floating or decimal numbers.īy default, the above 2 methods will not operate on decimal numbers. Hence we need to escape multiplication symbol like “\*”. Note: When we use “expr” multiplication symbol “*” is not escaped. Instead of using double brackets we can also use “expr” keyword to perform the operations. Then you need to add “$” symbol, to compute the arithmetic operations. To perform any arithmetic operations, you need to include them inside double brackets. I have taken 2 numbers, and will perform arithmetic operations: Hence we need to do any arithmetic separately as shown below: Point to be noted here is, echo command will output whatever is given to it as input. Simple way to add two numbers is given below: #!/bin/bash In this chapter we shall look at how to perform various arithmetic operations.Ĩ.2 Performing operations on floating or decimal numbers.Ĩ.3 Performing complex math operations 8.1 Arithmetic addition:
0 Comments
Leave a Reply. |