The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 02:56 PM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 11:29 AM
shell variable access sabina Shell Programming and Scripting 2 02-24-2005 11:43 AM
Need help to access/mount so to access folder/files on a Remote System using Linux OS S.Vishwanath UNIX for Dummies Questions & Answers 2 07-30-2001 08:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-19-2008
satish@123 satish@123 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 35
Post how to access a variable value

hi
i have a function
abc()
{
a=1
b=2
c=3
}
Now i want to assign these values to the variables out side the function
k=a
l=b
m=c

please help
  #2 (permalink)  
Old 05-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Variables have global scope, unless explicitly declared local inside a function.

You need to interpolate the values that you assign, though.

Code:
k=$a
l=$b
m=$c
  #3 (permalink)  
Old 05-19-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"
  #4 (permalink)  
Old 05-19-2008
jredx jredx is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
Quote:
Originally Posted by aju_kup View Post
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"
Included in a script and run, I get the following output
outside fn
outside fn
outside fn
  #5 (permalink)  
Old 05-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Works here (although I would perhaps have done it differently); which shell are you using?

Code:
bash$ cat jredx
#!/bin/sh
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"

vnix$ ./jredx
outside fn 1
outside fn 2
outside fn 3
Sponsored Links
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0