Unix / Linux - Shell Builtin Mathematical Functions

Hello, aspiring programmers! Today, we're going to embark on an exciting journey into the world of Unix/Linux shell builtin mathematical functions. Don't worry if you're new to programming - I'll guide you through each step with clear explanations and plenty of examples. Let's dive in!

Unix / Linux - Builtin Functions

What are Shell Builtin Functions?

Before we delve into the mathematical functions, let's understand what shell builtin functions are. These are commands that are built into the shell itself, meaning they don't need to be installed separately. They're always available at your fingertips, ready to help you perform various tasks efficiently.

Why Use Mathematical Functions in Shell Scripts?

You might wonder, "Why do we need math in shell scripts?" Well, imagine you're writing a script to calculate your monthly budget or to analyze some data. That's where these functions come in handy! They allow you to perform calculations right within your shell script, saving you time and effort.

Common Shell Builtin Mathematical Functions

Let's explore some of the most commonly used mathematical functions in Unix/Linux shells. We'll use the Bash shell for our examples, as it's widely used and comes pre-installed on many systems.

1. Basic Arithmetic Operations

Bash provides several ways to perform basic arithmetic. Let's look at them:

a. Using the expr command

#!/bin/bash
a=10
b=5
sum=$(expr $a + $b)
echo "The sum of $a and $b is: $sum"

This script will output: The sum of 10 and 5 is: 15

Here, expr is used to evaluate the expression $a + $b. The result is then stored in the sum variable.

b. Using double parentheses (( ))

#!/bin/bash
a=10
b=5
((sum = a + b))
echo "The sum of $a and $b is: $sum"

This method is more concise and often preferred for simple arithmetic operations.

2. More Complex Mathematical Operations

For more complex calculations, Bash provides the bc command. While not strictly a builtin, it's commonly available and very useful.

#!/bin/bash
pi=$(echo "scale=2; 22/7" | bc)
echo "The value of pi (to 2 decimal places) is: $pi"

Output: The value of pi (to 2 decimal places) is: 3.14

Here, scale=2 sets the number of decimal places, and bc performs the division.

Table of Commonly Used Mathematical Operations

Here's a handy table of mathematical operations you can perform in Bash:

Operation Symbol Example
Addition + echo $((5 + 3))
Subtraction - echo $((10 - 4))
Multiplication * echo $((6 * 7))
Division / echo $((20 / 5))
Modulus (Remainder) % echo $((17 % 3))
Exponentiation ** echo $((2 ** 3))

Advanced Mathematical Functions

Now that we've covered the basics, let's explore some more advanced mathematical functions. These aren't built into Bash itself, but they're commonly used in shell scripts through the bc command.

1. Square Root

#!/bin/bash
number=16
sqrt=$(echo "scale=2; sqrt($number)" | bc)
echo "The square root of $number is: $sqrt"

Output: The square root of 16 is: 4.00

2. Sine and Cosine

#!/bin/bash
angle=30
sin=$(echo "scale=4; s($angle * 3.14159 / 180)" | bc -l)
cos=$(echo "scale=4; c($angle * 3.14159 / 180)" | bc -l)
echo "The sine of $angle degrees is: $sin"
echo "The cosine of $angle degrees is: $cos"

Output:

The sine of 30 degrees is: .4999
The cosine of 30 degrees is: .8660

Note the -l option with bc, which loads the math library for trigonometric functions.

Practical Example: Temperature Converter

Let's put our newfound knowledge to use with a practical example - a temperature converter!

#!/bin/bash

echo "Temperature Converter"
echo "1. Celsius to Fahrenheit"
echo "2. Fahrenheit to Celsius"
read -p "Enter your choice (1/2): " choice

case $choice in
  1)
    read -p "Enter temperature in Celsius: " celsius
    fahrenheit=$(echo "scale=2; ($celsius * 9/5) + 32" | bc)
    echo "$celsius°C is equal to $fahrenheit°F"
    ;;
  2)
    read -p "Enter temperature in Fahrenheit: " fahrenheit
    celsius=$(echo "scale=2; ($fahrenheit - 32) * 5/9" | bc)
    echo "$fahrenheit°F is equal to $celsius°C"
    ;;
  *)
    echo "Invalid choice"
    ;;
esac

This script demonstrates how we can use mathematical functions in a real-world scenario. It allows users to convert temperatures between Celsius and Fahrenheit.

Conclusion

Congratulations! You've just taken your first steps into the world of shell scripting and mathematical functions. Remember, practice makes perfect. Try creating your own scripts using these functions - maybe a calculator or a script to analyze some data.

As you continue your journey in programming, you'll find that these basic mathematical operations form the foundation for more complex computations. Keep exploring, keep learning, and most importantly, have fun with it!

Happy coding, future tech wizards! ??‍??‍?

Credits: Image by storyset