LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-12-2009, 11:40 PM   #1
linuxgentoo
LQ Newbie
 
Registered: Nov 2007
Location: Shanghai,China
Distribution: gentoo
Posts: 19
Blog Entries: 1

Rep: Reputation: 0
How to make shell variable don't expand immediately


assumption:
X=/home/user
Y=${X}/tmp

When the X variable is changed,However the Y variable isn't changed according to X's change,Y already expand when define it.

Is there any way that expand variable when only referring Y variable ,not define it?

Thanks
 
Old 06-13-2009, 08:36 AM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,086

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Uhm.. this is not called expanding I believe. Expanding is when file* becomes file1 file2 file34 etc. IMHO.

As for your problem, Y get the value of $X when you say it has to get it, i.e. Y=$X gives the value of $X to Y, and no matter what happens to X afterwards, Y won't change. It is one of the fundamentals of programming in this type of languages.

In an ordinary programming language you would create a function getY or so which would look at $X at the moment of calling. But Bash doesn't provide function return values, at least not easily.

The only thing you can do is NOT using Y, but always ${X}/tmp when you need this string. You can beautify your program a bit more to define /tmp just once Z="/tmp", so if you have to change this value you'd only have to do it in one place. You reference would become: ${X}/${Z} wherever you use $Y now.

jlinkels
 
  


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
korn shell variable help 4play Programming 9 11-23-2011 12:03 PM
Shell scripting : how to name a variable with the value of another variable JimOrJoe Programming 9 06-15-2008 09:36 PM
shell variable sniffer_raghav Linux - Newbie 4 06-12-2008 05:34 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 12:49 PM
Variables in Make file do not expand in shell? realos Programming 5 08-07-2005 05:16 AM


All times are GMT -5. The time now is 12:47 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration