EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Unix Tutorial Functions in Unix
 

Functions in Unix

Priya Pedamkar
Article byPriya Pedamkar

Updated May 31, 2023

Functions in Unix

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Functions in Unix

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

Functions in Unix

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

Functions in Unix

Note: Sometimes, while executing the sh script, we may get the error as:

 Unix

This only has to do here by updating the file permissions.

unix

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.

command-line arguments

What if we don’t pass any arguments to the Function?

f7

And if we give only two arguments?

two arguments

Else, can we pass strings?

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

Addition

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

sum

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

Nested Functions

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

Nested Functions

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

Nested Functions

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

Inner Function

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.

  1. Unix Architecture
  2. Unix commands
  3. Uses of Unix
  4. UNIX interview questions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW