Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-12-2009, 11:50 PM   #1
Registered: Apr 2008
Posts: 30

Rep: Reputation: 15
functions in bash scripting


I would like to know if bash can only handle a function by itself or whether a fucntion can be used where it accepts a variable from the main program? From my reading on bash scripting sites it looks like the former is possible. below is an e.g. of what I mean:

#Script to clarify my above question about functions used in bash
#test function
if [$x == 5]; then
y=x+3 else
echo $y "is the total value"
#Main program lines here
echo "Welcome! Please type in a number between 1 and 9 inclusive"
read num
#Call the above function with the value of num passed to it
#end of script

I know the above might have some syntax errors regarding the if statement etc, but what's important to me is if I can pass variables to the function like I did above.

Old 05-13-2009, 01:10 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Fns certainly can take params:
Old 05-13-2009, 02:36 AM   #3
Senior Member
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
When I was reading abs I found it rather buggy and plenty of text was not related to bash directly. Maybe is it me only being overly captious.
Old 05-13-2009, 10:39 AM   #4
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,728

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
When writing a function in Bash you do NOT specify parameters in the function declaration. Inside the function you refer to parameters positionally, $1, $2 etc. These parameters are local to a function, hence not the command line parameters.

You pass parameters to a function by appending the call with parameters:
myfunc parm1 parm2
So if you have on the command line one parameter, and you want to pass that to you function:
myfunc $1
Run you program with sh -x youprogram while developing, it helps understand where the values go.

Old 05-14-2009, 06:22 PM   #5
Registered: Apr 2008
Posts: 30

Original Poster
Rep: Reputation: 15
Hi Guys

Thanks for all the help That was exactly what I was looking for.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Scripting Functions ishcoleobo Programming 8 03-26-2008 09:51 PM
Functions in bash script som_kurian Programming 3 01-01-2008 10:12 AM
Order of functions in a bash script essdeeay Programming 1 05-02-2006 11:52 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 02:48 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 PM.

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