I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
Shell Functions are used to specify the blocks of commands that may be repeatedly invoked at different stages of execution.
The main advantages of using Unix Shell Functions are to reuse the code and to test the code in a modular way.
This tutorial will explain to you all about Functions in Unix.
Unix Video #18:
Working with Functions in Unix
Shell functions typically do not return the result to the calling code. Instead, global variables or output streams are used to communicate the result. The variable ‘errno’ is often used to communicate whether a command ran successfully or not.
A number of commands also print out their result into the ‘stdout’ stream so that the calling function can read into a variable.
In this tutorial we will cover:
How to create functions
Passing parameters to a function
Returning a value from a function
Syntax for defining functions:
function_name()
{
…
<statements>
…
}
To invoke a function, simply use the function name as a command.
Example:
$ function_name
To pass parameters to the function, add space-separated arguments like other commands.
Example:
$ function_name $arg1 $arg2 $arg3
The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3, etc.
Example:
function_name()
{
…
c = $1 + $2
…
}
Functions can return values using any one of the three methods:
#1) Change the state of a variable or variables.
#2) Use the return command to end the function and return the supplied value to the calling section of the shell script.
Example:
function_name()
{
echo “hello $1”
return 1
}
Running the function with a single parameter will echo the value.
$ function_name ram
hello ram
Capturing the return value (stored in $?) as follows:
$ echo $?
1
#3) Capture the output echoed to the stdout.
Example:
$ var = `function_nameram`
$ echo $var
hello ram
Check our upcoming tutorial to know more about Text Processing in Unix.
Overview of Unix Shell Loops and Different Loop Types like: Unix Do While Loop Unix For Loop Unix Until Loop In this tutorial, we will cover the control instructions that are used to iterate a set of commands over a series of data. Unix offers three loop structures of which we can…
Introduction to Unix Shell Scripting: In Unix, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. Unix commands may also be executed non-interactively in the form of a Shell Script. The script is a series of…
Working with Shell Arithmetic and Boolean Operators in Unix: In this tutorial, we will review the various operators that are supported by the Unix shell. Operators are used for manipulating variables and constants in shell programs. They are required to perform mathematical operations. Here, we will explain to you more…
Overview of Unix Command Line Arguments: The Unix shell is used to run commands, and it allows users to pass run-time arguments to these commands. These arguments, also known as command line parameters, allow the users to either control the flow of the command or to specify the input data…
Overview of Pipes in Unix Programming: In this tutorial, we will learn more about Unix Pipes. Later, we will work with some of the remaining filter commands and see an example of piping them together. Unix Video #20: Pipes in Unix A series of filter commands can be piped together…
Introduction to Unix Operating System: Let's start with Tutorial #1: 'What is Unix' in this series. In this tutorial, you will be able to understand the basic concepts of operating systems, the features of Unix, along its Architecture. => Click here for the Complete Unix Tutorial series Unix Video #1:…
Overview of Unix Filters Text Processing Utilities: In this tutorial, we will learn about filters and work with various filter commands. Filters are commands that read input from stdin and write output to stdout. By default, when using a shell terminal, the stdin is from the keyboard, and the stdout is…
Overview of Unix Switch Case Statements: In this tutorial, we will see how a switch case block can be used when creating conditional flow depending on the various values of a single expression. Unix Shell The case-esac Statement Unix Video #16: The Shell Switch Case Syntax and Examples: case <word> in…