w3resource

Bash Functions: Exercises, Solutions & Explanations

1.

Greeting Function:

Write a Bash script that defines a function called greet that takes a name as an argument and prints a greeting message using that name.

Code:

# D#!/bin/bash

efine the greet function
greet() {
    local name="$1"  # Get the name from the first argument
    echo "Hello, $name! Welcome!"
}

# Call the greet function
greet "Maria"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Hello, Maria! Welcome!

Explanation:

In the exercise above,

  • Define a function called "greet()" using the greet() { ... } syntax.
  • Inside the function:
    • Use 'local' to declare a local variable 'name' to store the argument passed to the function.
    • Next use "echo" to print a greeting message containing the provided name.
  • Outside the function, we call "greet()" with the argument "Alice". You can replace "Alice" with any name you want to greet.

2.

Addition Function:

Write a Bash script that defines a function called add that takes two numbers as arguments and prints their sum.

Code:

#!/bin/bash

# Define the add function
add() {
    local num1=$1
    local num2=$2
    local sum=$((num1 + num2))
    echo "The sum of $num1 and $num2 is: $sum"
}

# Call the add function with two numbers
add 10 20
add 100 200

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The sum of 10 and 20 is: 30
The sum of 100 and 200 is: 300

Explanation:

In the exercise above,

  • Define a function called "add()" using the add() { ... } syntax.
  • Inside the function:
    • We declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • We calculate the sum of 'num1' and 'num2' and store it in the 'sum' variable.
    • Finally, we use "echo" to print a message showing the sum.

3.

Subtraction Function:

Write a Bash script that defines a function called subtract that takes two numbers as arguments and prints their difference.

Code:

#!/bin/bash

# Define the subtract function
subtract() {
    local num1=$1
    local num2=$2
    local difference=$((num1 - num2))
    echo "The difference between $num1 and $num2 is: $difference"
}

# Call the subtract function with two numbers
subtract 35 12
subtract 40 60

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The difference between 35 and 12 is: 23
The difference between 40 and 60 is: -20 

Explanation:

In the exercise above,

  • Define a function called "subtract()" using the subtract() { ... } syntax.
  • Inside the function:
    • Declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • Calculate the difference between 'num1' and 'num2' and store it in the 'difference' variable.
    • Finally, use "echo" to print a message showing the difference.

4.

Multiplication Function:

Write a Bash script that defines a function named multiply that takes two numbers as arguments and prints their product.

Code:

#!/bin/bash

# Define the multiply function
multiply() {
    local num1=$1
    local num2=$2
    local product=$((num1 * num2))
    echo "The product of $num1 and $num2 is: $product"
}

# Call the multiply function with two numbers
multiply 12 6
multiply 100 -20

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The product of 12 and 6 is: 72
The product of 100 and -20 is: -2000

Explanation:

In the exercise above,

  • Define a function called "multiply()" using the multiply() { ... } syntax.
  • Inside the function:
    • Declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • Calculate the product of 'num1' and 'num2' and store it in the 'product' variable.
    • Finally, use "echo" to print a message showing the product

5.

Division Function:

Write a Bash script that defines a function called divide that takes two numbers as arguments and prints their division.

Code:

#!/bin/bash

# Define the divide function
divide() {
    local num1=$1
    local num2=$2

    # Check if num2 is zero to avoid division by zero error
    if [ $num2 -eq 0 ]; then
        echo "Error: Division by zero"
        exit 1
    fi

    local result=$(bc <<< "scale=2; $num1 / $num2")
    echo "The division of $num1 by $num2 is: $result"
}

# Call the divide function with two numbers
divide 24 5
divide 13 330

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The division of 24 by 5 is: 4.80
The division of 13 by 330 is: .03 

Explanation:

In the exercise above,

  • Define a function called "divide()" using the divide() { ... } syntax.
  • Inside the function:
    • Declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • Check if 'num2' is zero to avoid the division by zero error. If it is, we print an error message and exit the script.
    • Use "bc" command-line calculator to perform floating-point division with two decimal places (scale=2).
    • Finally print a message showing the result of the division.

6.

Factorial Function:

Write a Bash script that implements a function called factorial that calculates and prints the factorial of a given number.

Code:

#!/bin/bash

# Define the factorial function
factorial() {
    local num=$1
    local result=1

    # Check if num is negative
    if [ $num -lt 0 ]; then
        echo "Error: Factorial is not defined for negative numbers"
        exit 1
    fi

    # Calculate factorial
    for ((i = 1; i <= num; i++)); do
        result=$((result * i))
    done

    echo "The factorial of $num is: $result"
}

# Call the factorial function with a number
factorial 5
factorial 10

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The factorial of 5 is: 120
The factorial of 10 is: 3628800 

Explanation:

In the exercise above,

  • Define a function called "factorial()" using the factorial() { ... } syntax.
  • Inside the function:
    • Declare a local variable 'num' to store the argument passed to the function.
    • Declare another local variable 'result' and initialize it to 1.
    • Check if 'num' is negative. If it is, we print an error message and exit the script.
    • Finally calculate the factorial of 'num' using a loop and store the result in the 'result' variable.

7.

Maximum Function:

Write a Bash script that defines a function named maximum that takes two numbers as arguments and prints the maximum of the two.

Code:

#!/bin/bash

# Define the maximum function
maximum() {
    local num1=$1
    local num2=$2

    if [ $num1 -gt $num2 ]; then
        echo "The maximum of $num1 and $num2 is: $num1"
    else
        echo "The maximum of $num1 and $num2 is: $num2"
    fi
}

# Call the maximum function with two numbers
maximum 100 115
maximum -5 -9

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The maximum of 100 and 115 is: 115
The maximum of -5 and -9 is: -5

Explanation:

In the exercise above,

  • Define a function called "maximum()" using the maximum() { ... } syntax.
  • Inside the function:
    • Declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • Use an if-else statement to compare 'num1' and 'num2'.
    • If 'num1' is greater than 'num2', we print a message showing 'num1' as the maximum.
    • Otherwise, print a message showing 'num2' as the maximum.

8.

Minimum Function:

Write a Bash script that defines a function called minimum that takes two numbers as arguments and prints the minimum of the two.

Code:

#!/bin/bash

# Define the minimum function
minimum() {
    local num1=$1
    local num2=$2

    if [ $num1 -lt $num2 ]; then
        echo "The minimum of $num1 and $num2 is: $num1"
    else
        echo "The minimum of $num1 and $num2 is: $num2"
    fi
}

# Call the minimum function with two numbers
minimum 100 115
minimum -12 -15 

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The minimum of 100 and 115 is: 100
The minimum of -12 and -15 is: -15 

Explanation:

In the exercise above,

  • Define a function called "minimum()" using the minimum() { ... } syntax.
  • Inside the function:
    • Declare two local variables 'num1' and 'num2' to store the arguments passed to the function.
    • Use an if-else statement to compare 'num1' and 'num2'.
    • If 'num1' is less than 'num2', we print a message showing 'num1' as the minimum.
    • Otherwise, print a message showing 'num2' as the minimum.

9.

Square Function:

Write a Bash script that defines a function named square that takes a number as an argument and prints its square.

Code:

#!/bin/bash

# Define the square function
square() {
    local num=$1
    local square=$((num * num))
    echo "The square of $num is: $square"
}

# Call the square function with a number
square 12
square 34

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The square of 12 is: 144
The square of 34 is: 1156

Explanation:

In the exercise above,

  • Define a function called "square()" using the square() { ... } syntax.
  • Inside the function:
    • Declare a local variable 'num' to store the argument passed to the function.
    • Calculate the square of 'num' and store it in the 'square' variable.
    • Finally, use "echo" to print a message showing the square.

10.

Power Function:

Write a Bash script that implements a function called power that takes two numbers as arguments and prints the result of raising the first number to the power of the second number.

Code:

#!/bin/bash

# Define the power function
power() {
    local base=$1
    local exponent=$2
    local result=$((base ** exponent))
    echo "The result of $base raised to the power of $exponent is: $result"
}

# Call the power function with two numbers
power 2 5
power 100 2

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
The result of 2 raised to the power of 5 is: 32
The result of 100 raised to the power of 2 is: 10000

Explanation:

In the exercise above,

  • Define a function called "power()" using the power() { ... } syntax.
  • Inside the function:
    • Declare local variables 'base' and 'exponent' to store the arguments passed to the function.
    • Calculate the result of raising 'base' to the power of 'exponent' using the ** operator.
    • Finally, use "echo" to print a message showing the result.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.