Updated May 31, 2023
Introduction to Functions in Unix
The routines contain a set of instructions that act as a separate unit in the entire program code, intended for serving a certain purpose, facilitating the performing of complex tasks, and syntactically written inside an indented block. That is also useful in optimizing the program performance and allows the programmers to build functionalities that can be performed right from the simplest to the most complex tasks termed functions in Unix. This concept assumes complexity based on the nature of the purpose to be addressed using them.
Syntax of Functions in Unix
The creation of Functions follows the basic syntax code as follows. Sample example of a Function.
Code
First()
{
echo " First Program for defining functions ";
echo " Yayyy! ";
}
Here, we have written two echo statements inside our First Function. For calling the Function, we can call by the Function name.
Output
Passing Parameters to Functions in Unix
We have successfully made a simple Function, called it, and executed the required commands. Now, what if I need to add two numbers through user inputted values? So, here we would see how we can write a function with arguments. The basic syntax would be the same. Example of parameterized function
Code
Second()
{
echo "Enter your name: ";
read name;
echo "Hi $name , have a good day";
}
Output
It will prompt us to enter a name and wish them
In the same way, we can even create a script file. Let’s write an addition Function inside a shell script and see how it works.
Code
vi add.sh
echo "enter first number: ";
read a;
echo "enter the second number: ";
read b;
sum=$((a+b));
echo " Result is $sum";
*********************
Save the file with: wq!
Output
This only has to do here by updating the file permissions.
We can even execute the sh file using the command by . /add.sh
In the same way, we can even execute a Function by providing command-line arguments. So, let’s see how we can do this by adding three numbers.
Code
<Addition()
{
number1=$1;
number2=$2;
number3=$3;
sum=$((number1+number2+number3));
echo "Sum is $sum";
}
Now we can pass our numbers as command-line arguments and get our required output.
What if we don’t pass any arguments to the Function?
And if we give only two arguments?
Else, can we pass strings?
We can even have our code echo out an error when invalid arguments are passed. Below, let’s see how we can ask the user to input arguments if no command-line arguments are passed.
Code
Add()
{
number1=$1;
number2=$2;
if [ $# -ne 2 ]; then
echo “ Enter two numbers to add “;
fi
sum=$((number1+number2));
echo " Sum is : $sum ";
}
Output
If we don’t pass any arguments to our Function, it gives our echo where we have asked to provide two command-line numbers and then give the sum output. It does so, as we did not tell the Function to stop if we encounter the error. In the second case, we can see the output giving the sum properly without any intermediate echo. We can even use return commands in returning out values for these Unix Functions. The value in the return statement is being stored in $?
Output
As an exercise, can you try writing a function that breaks out if we don’t pass the arguments?
Nested Functions in Unix
By naming, we can Figure out that Nested Functions are about defining a function inside a Function. Let’s see how we can do that now. We are writing this inside a shell script for easier understanding.
Code
vi Nested_Function.sh
***** Code Inside the sh Function*******
#!/bin/sh
First()
{
echo " Inside First Function ";
echo " Calling the second function below ";
echo "**********";
Second
echo "**********";
echo " After calling Second function ";
}
Second()
{
echo " Inside Second Function";
echo " Done ";
}
First
************************
Save the file with: wq!
Output
In the above example, we just called the second Function inside the first. Let’s now write a function inside the first function only.
Code
vi Nest_Func1.sh
***** Code Inside the sh Function*******
First_One()
{
echo " Inside the function First_One";
echo " Below is the inner function ";
Second_One()
{
echo " ********** ";
echo " Inside Inner function";
echo " Completed Second Function";
echo " ********** ";
}
echo "Done with second example too";
}
First_One
Second_one
************************
Save the file with: wq!
Now, what output do you expect? Do you think both the details of the first and second Functions would be displayed?
Output
We can clearly see that the Function did not call the inner function inside it. So, what’s the problem here?
Inside our script, we have called only the first Function. Now try calling the second Function too. As observed here, we cannot call the Function by itself. In the above code, we have highlighted the added code.
Output
Notice that the echo we have written after the execution of the Second Function has come before itself. Now, if we try calling the second Function first, followed by the First one?
Output
We can clearly understand that the inner function is not being called until the main/first Function is called. As an exercise, can you try the addition of two numbers using nested Functions? (One for inputting user values and others to add the numbers)
Conclusion
So, this is how we can define Functions in Unix. And one major advantage of Unix I found was the error in the output can be easily understood and can help to rectify our code accordingly. Try executing those Functions and use the return command too. Have happy learning in Unix.
Recommended Article
We hope that this EDUCBA information on “Functions in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.