The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need to return value from function darshakraut Shell Programming and Scripting 5 05-14-2008 05:13 AM
function return array dophine Shell Programming and Scripting 6 05-06-2008 09:16 AM
Function call not return value into var katzs500 UNIX for Advanced & Expert Users 4 04-21-2008 10:27 AM
Function call not return value into var katzs500 UNIX for Dummies Questions & Answers 1 04-18-2008 06:31 PM
ORACLE return a function to Solaris Rafael.Buria Shell Programming and Scripting 1 10-23-2007 02:38 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-22-2007
Registered User
 

Join Date: Aug 2007
Posts: 7
return value of a function

Hi
I have a doubt in the way the variables inside a function are treated .
if a function is called from the main script directly, the variables inside them act as global variables.
however if the return value of the function is stored to some other variable in the main script as shown,
x=`some_function $some_args`
then all the variables in the function become local ones. they are not relected in the main script.

thnx in advance....
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-22-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,418
That is because `somefunc somearg` is run in backtics - as a child process. Any changes to variables are not seen in the calling (parent) process.
Reply With Quote
  #3 (permalink)  
Old 08-22-2007
Registered User
 

Join Date: Aug 2007
Posts: 7
so why it is not run as a child process when function is called directly in the main script
Reply With Quote
  #4 (permalink)  
Old 08-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by prez View Post
so why it is not run as a child process when function is called directly in the main script
1. because that is what shells do.

2. It's more useful as so you *can* have functions that change global variables.

3. It means it does not have to fork() another process to do the job.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0