LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2009, 07:18 AM   #1
amemyum
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Rep: Reputation: 0
can we use variable inside another variable


Hi,

I am facing this problem where I have declared the variable ($ProgramID) and again I'm callin this variable in another variable ($SOURCEPATH)


here the extract from my code:
SOURCEPATH=../sqa/$ProgramID/seatapps/app_dir

So, I called this variable SOURCEPATH the variable "ProgramID" does not get updated with the actual value.

thanks
 
Old 11-10-2009, 08:05 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please make sure to specify what platform you're referring to. I assume we're talking about bash scripting?

Yes, you can do it. Using variables to set other variables is a very common practice.

Your code should be working. I can't see any problems with it. Do make sure you aren't using single-quotes around the string, because that would keep the variable from expanding. I do suggest enclosing it in double-quotes though.

To see what your script is doing, you can use "set -x" and "set +x" to turn verbose output on and off, and will show you what substitutions are being made. A few echos liberally spaced around the code can also help.

Code:
set -x

ProgramID=123

echo "$ProgramID"

SOURCEPATH="../sqa/$ProgramID/seatapps/app_dir"

echo "$SOURCEPATH"

set +x
This works fine for me.
If you can, please post a longer sample of the code so we can see what's happening in context.

Last edited by David the H.; 11-10-2009 at 08:06 AM.
 
Old 11-10-2009, 08:27 AM   #3
amemyum
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
thanks..
However, my problem is that the variable is set in another file and I'm trying to call the variable in another file and this way its not working

config.ini initializes the variable -> SOURCEPATH=../sqa/$ProgramID/seatapps/app_dir

month_files.sh calls-> cp $SOURCEPATH/*.air disks/seatapp/

thanks
 
Old 11-10-2009, 08:35 AM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
And how are you calling config.ini from month_files.sh?

Forrest
 
Old 11-10-2009, 08:41 AM   #5
amemyum
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
The file config.ini is read by file named main.sh and through a function call the other file month_files.sh is used.
 
Old 11-10-2009, 08:45 AM   #6
dunkar70
LQ Newbie
 
Registered: Nov 2009
Location: Chicago, IL
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
Variable scope

The scope of a variable is limited to the file in which it is defined. In order to get that value, you need to do one of the following:
1. incorporate a way of passing the value from the external script
2. set an environment variable with the external script
3. incorporate the code from the other script

One way to pass the value is to have the source script return the value from a call like the example below. I have not tested this, but it should work...in theory. Anyone and everyone who knows better is free to correct me. The problem with this approach is that it may require you to modify the source script in which you are defining the variable.

script1:
Code:
#!/bin/bash
var1=SomeValue
return $var1
script2:
Code:
#!/bin/bash
var2=`/bin/bash script1`
echo $var2
Note: The command to define var2 is using back ticks (`) not single quotes.

Having said all that... unless you are creating a fairly large package, it may be simpler and safer to keep each script self-contained and independent of your other scripts. This way you do not need to worry about dependencies and breaking things if you have to modify one script.
 
Old 11-10-2009, 08:50 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Well, no wonder then. It helps to include little details like that when diagnosing a problem, you know. The more relevant information you provide at the beginning, the more likely you are to get help.

It's impossible to use a script to set a variable in the shell running above it. Each script you call runs in it's own subshell, and the variables set inside them are only available to that process and any further subprocesses it creates.

You'll probably have to either alter your first script so that it exports the value to a temporary file for the second script to read, or run the first script directly inside the second script, so that its output can be used directly.
 
Old 11-10-2009, 08:55 AM   #8
dunkar70
LQ Newbie
 
Registered: Nov 2009
Location: Chicago, IL
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
ah yes... I missed that option...

Quote:
Originally Posted by David the H. View Post
You'll probably have to either alter your first script so that it exports the value to a temporary file for the second script to read, or run the first script directly inside the second script, so that its output can be used directly.
I generally prefer to avoid temporary files as they tend to make things messy if they are not deleted properly, overwrite existing temporary files, or collide with other people trying to do the same thing on the box. For complex scripts, they can also be difficult to debug if they are deleted or are using randomly generated names (to avoid overwriting an existing temp file).

Update: Of course, using environment variables has the same problems in addition to using additional memory.

Last edited by dunkar70; 11-10-2009 at 09:04 AM. Reason: Adding clarity
 
Old 11-16-2009, 07:19 AM   #9
amemyum
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
thanks david & dunkar...
 
Old 11-16-2009, 07:45 AM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
script1
Code:
#!/bin/bash
var1=SomeValue
echo $var1
script2
Code:
#!/bin/bash
var2=$(script1)
echo $var2
Or anther way

script1
Code:
var1=SomeValue
script2
Code:
#!/bin/bash
. script1
echo $var1
Or a third way.
script1
Code:
#!/bin/bash
export var1=SomeValue
script2
Code:
#!/bin/bash
echo $var1
Call this way
Code:
script1
script2

Evo2.

Last edited by evo2; 11-16-2009 at 07:51 AM. Reason: 3rd way
 
Old 11-16-2009, 12:28 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
No, that third way will not work, as has been explained already in this thread. Exporting the variable makes no difference. There's simply no way a script can set a variable outside its own subshell environment. Once the first script terminates, any variables set within it are lost, and are inaccessible from outside in any case (except through stdout or a similar redirection). Try it yourself and see.
 
Old 11-16-2009, 12:58 PM   #12
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
The dot, however, does run the script in the current context, and is used extensively by the system scripts to get parameter values into a running script. So evo2's second way is, I believe, the way to go.

You might also want to look at the exec function, and the differences between $(...) and ${...}, although the latter - in the ${!..} form - is used for referencing the value of a variable defined by the value of another variable. (I.e., "indirection.") It's "useful" for writing self-modifying bash scripts, if you have some need for that functionality.
 
Old 11-16-2009, 01:35 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
A variable can be set in a calling script via the eval command if the called script writes a variable assignment. For example ...

script1.sh:
Code:
eval $(script2.sh)
script2.sh:
Code:
echo foo=bar
 
Old 11-16-2009, 04:44 PM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by David the H. View Post
No, that third way will not work, as has been explained already in this thread. Exporting the variable makes no difference. There's simply no way a script can set a variable outside its own subshell environment. Once the first script terminates, any variables set within it are lost, and are inaccessible from outside in any case (except through stdout or a similar redirection). Try it yourself and see.
True. No need to try, just need to read what I typed :-)

Should have been:
script1
Code:
export var1=SomeValue
Run like
Code:
. scrpit1
script2
Evo2.
 
  


Reply


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
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
Using Shell variable inside the C program Varsha_vdd Linux - Newbie 5 06-17-2008 10:42 AM
A problem with a shell variable visibility inside a while crisostomo_enrico Programming 7 11-23-2007 06:29 AM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 07:48 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 09:50 PM


All times are GMT -5. The time now is 07:03 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration