Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Linux
3.
Shell Functions
4.
Defining Shell Functions
5.
Passing Parameters
6.
Return Values
7.
Nested Function Call
8.
Frequently Asked Questions 
8.1.
What are shell functions in Linux?
8.2.
What is the difference between return and exit commands?
8.3.
How can we define shell functions? 
8.4.
Who introduced Linux, and in which year?
8.5.
What is the use of the echo command?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux - Shell Functions

Author Ayushi Goyal
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Every task you do on Linux can be done using commands. But what if you want to execute a group of commands and store them for later execution? All of this can be done using shell functions in Linux.

Introduction

This article will discuss the ways of using arrays in Linux. What are the different ways of declaring and accessing an array? And at last, we will perform various operations like search, count, and delete elements from Linux Arrays. But before discussing shell functions, let's first get familiar with Linux.

What is Linux

Linux is an open-source community like the Unix operating system. The initial release of Linux was on September 17, 1991, by Linus Torvalds.

Since its release, Linux has been used everywhere, whether on smartphones, watches, or supercomputers. 

What is Linux

It is quite a helpful tool for regular computer users or developers and can be used in the following ways:

  • Software development: Linux consists of the most widely used tools for software development. For example, emacs, vim, compilers, git, etc. 
     
  • Embedded device: for systems that require limited computing function. Linux acts as an embedded Operating system in various applications, like household appliances, network file systems, entertainment systems, etc. 
     
  • SuperComputers: All the current supercomputers are installed to run on one or another version of Linux. 
    Also read  features of linux operating system
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Shell Functions

A function contains a chunk of code that can be used repeatedly. The code reusability feature of OOPS(Object Oriented Programming) is achieved using functions. The functions enable the programmer to decompose a larger problem into small chunks and group them for later use.  

shell functions

Shell functions in Linux are similar to functions in other programming languages. These are also used to group commands for later execution. There is no special syntax for their execution; these functions are also executed like regular Linux commands. Wherever the name of a shell function is used, the list of commands associated with it is executed. 

Let's now discuss the syntax to create a shell function with an example. 

Defining Shell Functions

Shell functions in Linux are declared using this syntax: 

<function_name> ()
{
     commands
}


Replace the <function_name> with the name of the function you want to give. Let's create a shell function named "Greeting" to say, "Welcome to Coding Ninjas." 

Example:

# Define a function "Greeting"
Greeting()
{
    echo "Welcome to Coding Ninjas"
}

# Execute the function
Greeting


Output:

Output 1

Passing Parameters

Parameter passing refers to passing input parameters to the function. Shell functions in Linux can also be defined to accept parameters while calling them. Parameters are represented using $1, $2, $3, and so on. 

$1 represents the first parameter 

$2 represents the second parameter

$3 represents the third parameter, and so on.  

Let's have an example of printing the names of 4 students.  
 

Example:

# Define a function "Names"
Names()
{
    echo "Student 1 name : $1"
    echo "Student 2 name : $2"
    echo "Student 3 name : $3"
    echo "Student 4 name : $4"
}

# Call function passing 4 parameters
Names John Davis Adam Irwin


Output:

Output2

Return Values

Return command is used to come out of the function declaration. Unlike the exit command, this return command does not terminate the whole program. Question mark (?) captures the value returned by the last executed command. The syntax to be followed is: return <code>

<code> can be anything to be returned to the point where the shell function is called. 

Let's see an example of returning the sum of two numbers. 


Example: 

# Define a function "Addition"
Addition()
{
    sum=`expr $1 + $2`
    return $sum
}

# Read two numbers from the user
echo "Enter number 1 : "
read num1

echo "Enter number 2 :"
read num2

# Call the function 
Addition $num1 $num2
echo "Sum of numbers - $?"


Output:

output3

Nested Function Call

Shell functions in Linux can call themselves and other functions. These functions are known as recursive functions (a function calling itself). 

Let's create two functions, 1st one to calculate the sum and 2nd one to call 1st one and print the sum. 
 

Example:

# Define a function "Addition"
Addition()
{
    sum=`expr $1 + $2`
    return $sum
}

function1()
{
    echo "Call Addition function"
    # Call another function
    echo "Enter number 2 : "
    read num2
    Addition $1 $num2
    echo "Sum = $?"
}

# Take user input
echo "Enter a number : "
read num
function1 $num


Output:

output4


These are the main operation performed using shell functions in Linux. Let's now discuss some frequently asked questions related to our topic. 

Frequently Asked Questions 

What are shell functions in Linux?

Shell functions in Linux are similar to functions in other programming languages. These are used to group commands for later execution. 

What is the difference between return and exit commands?

The return command terminates the function block, while the exit command terminates the whole program. 

How can we define shell functions? 

To define a shell function, you need to give the function name followed by brackets () and curly brackets to include a group of commands { commands }. The syntax is : function_name() { commands } 

Who introduced Linux, and in which year?

The initial release of Linux was on September 17, 1991, by Linus Torvalds.

What is the use of the echo command?

The Echo command prints the given text on the screen as output. 

Conclusion

In this blog, we learned about shell functions in Linux. This article introduces Linux and shell functions, followed by the way to declare, pass the parameter, and return the value from the function. We also discussed examples of each of these operations.  
 

If you found this blog has helped you enhance your knowledge about shell functions in Linux, and if you want to learn articles like this, check out our articles below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass