UNIX / Linux Shell Functions - Unix/Linux

What are UNIX / Linux Shell Functions?

In this section, we will consider in detail about the shell functions. Functions allow you to break down the overall functionality of a script into smaller, consistent subsections, which can then be called upon to execute their entity tasks when essential.

Using functions to perform recurring tasks is an outstanding way to generate code reuse. This is an important part of modern object-oriented programming principles.

Shell functions are related to subroutines, events, and functions in other programming languages.

Creating Functions

To state a function, basically use the following syntax –

The name of your function is function name, and that's what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.


Following instance shows the use of function –

Upon execution, you will receive the following output –

Pass Parameters to a Function

You can classify a function that will recognize parameters while calling the function. These parameters would be representing by $1, $2 and so on.

Following is an instance where we pass two parameters Zara and Ali and then we confine and print these parameters in the function.

Upon execution, you will obtain the following result –

Returning Values from Functions

If you perform an exit command from inside a function, its consequence is not only to end execution of the function but also of the shell program that called the function.

If you in its place want to just terminate implementation of the function, then there is way to come out of a defined function.

Base on the condition you can return any value from your function using the return command whose syntax is as follows –

Here code can be anything you decide here, but visibly you should desire something that is meaningful or useful in the environment of your script as a whole.


Following function returns a value 1 –

Upon effecting, you will accept the following result –

Nested Functions

One of the more attractive features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.

Following instance demonstrate nesting of two functions –

In the lead implementation, you will accept the following result −

Function Call from Prompt

You can put definitions for normally used functions inside your .profile. This definition will be available whenever you log in and you can use them at the command prompt.

Instead, you can group the definitions in a file, say test.sh, and then perform the file in the present shell by typing –

This has the result of causing functions defined inside test.sh to be read and define to the present shell as follows –

To eliminate the definition of a function from the shell, use the unset command with the .f selection. This command is also used to eliminate the definition of an inconsistent to the shell.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics