Passing Parameter to User Define Function in Unix

PREV     NEXT

  • Shell functions have their own command line argument.
  • Use variable $1, $2..$n to access argument passed to the function.

syntax :

name()

{

arg1=$1


arg2=$2

command on $arg1

}


To invoke the function, use the following syntax:

name java  linux

Where,

name = function name.

java = Argument # 1 passed to the function (positional parameter # 1).

linux= Argument # 2 passed to the function.

Ex:

Output:

  • All Arguments to function demo(): java C++ C#
  • First argument is java
  • Second argument is C++
  • Third argument is C#

     PREV     NEXT

Share this website !!!
Facebook Twitter Linkedin