Shell Functions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Shell Functions

With the help of functions, overall functionality of a function can be divided into smaller or logical parts, which can be called to perform their task. It helps us to check our program part by part. We can reuse the function where ever we want.

To create a function


  1.  function functionName () {  
  2.         Commands to be executed  
  3. }  

You will call your function with their function name.


  1. function Welcome () {  
  3. }  

Linux Shell function 1

Look at the above snapshot, we have created a function Welcome. We have called it in the above script.

Linux Shell function 2

Look at the above snapshot, on executing the above script, function Welcome is called.

Passing Parameters

You can pass one or more parameters in a function. Parameters will be defined as $1, $2 and so on.

Linux Shell function 3

Look at the above snapshot, $1 is a parameter passed to the function.

Linux Shell function 4

Look at the above snapshot, script produces the above output.