Register
It is currently Wed Aug 20, 2014 4:25 pm

Function Parameter by Reference


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Jan 27, 2010 10:04 pm   

Joined: Wed Jan 27, 2010 9:34 pm
Posts: 2
Am looking for a way to send a variable (by reference) to a function - the function can then change the value of that variable. I found this:
Code:
#!/usr/bin/bash

function setToXXX() {
  echo changing value of $1
  eval "$1=XXX"
}

FOO=hello
BAR=goodbye

setToXXX FOO
setToXXX BAR

echo after function, FOO is $FOO
echo after function, BAR is $BAR


If I change 'eval' line to ' eval "$1=XXX YYY" ' , then I get an error. Is there a way to do this?


Top
 Profile  
 PostPosted: Thu Jan 28, 2010 12:03 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
Code:
eval "$1=\"XXX YYY\""


Top
 Profile  
 PostPosted: Fri Jan 29, 2010 10:29 am   

Joined: Wed Jan 27, 2010 9:34 pm
Posts: 2
Perfect, thank you!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP