- Shell functions have their own command line argument.
- Use variable $1, $2..$n to access argument passed to the function.
syntax for Passing Parameter to User Define Function in Unix:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/sh demo() { echo "All Arguments to function demo(): $*" echo "First argument is $1" echo "Second argument is $2" echo "Third argument is $3" return } demo java C++ C# |
Output:
- All Arguments to function demo(): java C++ C#
- First argument is java
- Second argument is C++
- Third argument is C#