LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing parameters to functions in shell script (https://www.linuxquestions.org/questions/programming-9/passing-parameters-to-functions-in-shell-script-367897/)

kushalkoolwal 09-28-2005 02:31 PM

passing parameters to functions in shell script
 
Hi guys, I know we can write fucntions in shell script and call them with the following syntax:

function-name ()
{
command1
command2
....
....
}

and to call it we just give the command:

fucntion-name.


Now, can we pass parameters to functions by name/value just as we do it in C language? If yes, then How do we do that?

Thanks

druuna 09-28-2005 02:40 PM

Hi,

Bash (ksh) isn't that strickt when it comes to functions. Unless specifically set, variables are also known inside functions. Using $1, $2 etc also works.

Code:

#!/bin/bash

function callMe()
{
  echo "\$1 : $1"
  echo "\$varOne : $varOne"
  echo "\$2 : $2"
  echo "\$varTwo : $varTwo"
}

varOne="some text"
varTwo="100"

callMe "$varOne" "$varTwo"

When the function is called without variables, $1, $2 cannot be used.

Hope this helps.


All times are GMT -5. The time now is 12:58 AM.