TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Unix:How to Use functions in shell script

Sourav Gulati
Site Owner
Posts: 83

User defined fucntions can be created in shell scripts. Syntax for creating a function is :

Syntax:

function_name()

{

#code

}


Functions needs to be created before they are called.


Syntax for calling a function in shell script is :


function_name arg1 arg2


Also, command line arguments works similarly they work in shell  scripts. So $1 would be the first argument inside the function , $2 would be the second argument and so on.


Following is an example shell script which contains a fucntion "func()" :


$ cat testfunction.sh

#!/bin/bash

func()

{

echo $1

}

func hello

 

$ ./testfunction.sh

hello

 

 



--


March 14, 2013 at 8:58 AM Flag Quote & Reply

You must login to post.