LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 02:31 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
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

Last edited by kushalkoolwal; 09-28-2005 at 04:30 PM.
 
Old 09-28-2005, 02:40 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 09-28-2005 at 02:42 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 11:44 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM
passing java parameters to a unix script nephilim Programming 25 10-22-2003 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration